標籤:des style blog color art io
1,限制只能輸入數字
private void txtSize_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != 8 && e.KeyChar != 13 && !char.IsDigit(e.KeyChar) && e.KeyChar != 46) { MessageBox.Show("請輸入數字"); e.Handled = true; } }
2,限制只能輸入大小寫字母
private void txtChar_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != 8 && e.KeyChar != 13 && (!char.IsUpper(e.KeyChar) && !char.IsLower(e.KeyChar))) { MessageBox.Show("請輸入字母。"); e.Handled = true; } }
3,限制輸入1位字母
private void txtClassCode_KeyPress(object sender, KeyPressEventArgs e) { if (frmStart != "IsEdit") { if (e.KeyChar != 8 && e.KeyChar != 13 && (!char.IsUpper(e.KeyChar) && !char.IsLower(e.KeyChar))) { MessageBox.Show("請輸入字母。"); e.Handled = true; } if (e.KeyChar != 8 && e.KeyChar != 13 && (txtClassCode.Text.Length - txtClassCode.SelectedText.Length > 0)) { MessageBox.Show("只允許輸入一位字母。"); e.Handled = true; } } }
4,限制輸入內容長度
private void SetTextLen() { this.txtClassCode.MaxLength = 30; this.txtClassName.MaxLength = 100; this.txtNnemonicCode.MaxLength = 128; this.txtSpellCode.MaxLength = 255; this.txtDescription.MaxLength = 255; this.txtRemark.MaxLength = 255; }
然後在表單預設載入事件中調用此方法。
註:這樣設定的文字框輸入長度限制,在手動輸入情況下是生效的。但是如果是程式賦值給文字框,則長度限制會不起作用。