using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace c7_3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { label1.Text = "最多只能輸入10個字元"; } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { //取得目前textbox1全部字串 string s1 = textBox1.Text; int len = s1.Length; //取得輸入字元 char c1 = e.KeyChar; if (len < 10) { //(1)只顯示小寫英文字 //方法:If c1 <= "z" And c1 >= "a" Then if (c1 >= 'a' && c1 <= 'z') { textBox3.Text += c1.ToString(); } //(2)轉換成小寫英文字 //步驟1:先把字元轉換成大寫:c1.ToString().ToUpper() //步驟2:然後使用string.compare(x, y)函數,做比較 //注意:c#的字串比較,不能夠用"Z" > "A"的直接比較,必須用函數比較===> string.Compare("Z", "A") if (string.Compare(c1.ToString().ToUpper(),"A") >=0 && string.Compare(c1.ToString().ToUpper(), "Z") <= 0) //if(c1 > 'A') { //轉換成小寫顯示 textBox2.Text += c1.ToString().ToLower(); } //(3)只顯示數字 //方法:If c1 <= "9" And c1 >= "0" Then if (c1 >= '0' && c1 <= '9') { textBox4.Text += c1.ToString(); } } else { //如何將按下的鍵盤數值將不會作用在textbox1上:e.Handled = True e.Handled = true; } } } }