[英]How to make Textbox only accept alphabetic characters
我有一個帶有maskedtextbox
控件的Windows窗體應用程序,我只想接受字母值。
理想情況下,這將表現為按下除字母鍵之外的任何其他鍵將不產生結果或立即向用戶提供關於無效字符的反饋。
在每個可以想象的編程論壇上,這個問題可能已被問及並回答了一百萬次。 所提供的每個答案都具有與所述要求不同的區別。
由於您使用的是MaskedTextBox
,因此您可以使用其他驗證功能,而不需要處理按鍵。 您可以簡單地將Mask屬性設置為“L”(需要字符)或“?” (可選字符)。 為了向用戶顯示輸入不可接受的反饋,您可以使用BeepOnError
屬性或添加工具提示來顯示錯誤消息。 應該在MaskedInputRejected
事件處理程序中實現此反饋機制。
MaskedTextBox
控件提供ValidatingType
屬性來檢查傳遞Mask的要求的輸入,但可能不是正確的數據類型。 在此類型驗證后引發TypeValidationCompleted
事件,您可以處理它以確定結果。
如果您仍然需要處理按鍵事件,請繼續閱讀......!
在你的情況下,我建議的方法是,不是處理KeyDown
事件(表面上你不需要高級鍵處理功能)或使用正則表達式匹配輸入(坦率地說,矯枉過正),我只是使用內置的屬性Char結構。
private void maskedTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
Char pressedKey = e.KeyChar;
if (Char.IsLetter(pressedKey) || Char.IsSeparator(pressedKey) || Char.IsPunctuation(pressedKey))
{
// Allow input.
e.Handled = false
}
else
// Stop the character from being entered into the control since not a letter, nor punctuation, nor a space.
e.Handled = true;
}
}
請注意,此代碼段還允許您處理標點和分隔符鍵。
從MSDN (此代碼顯示如何處理KeyDown事件以檢查輸入的字符。在此示例中,它僅檢查數字輸入。您可以修改它以便它可用於字母輸入而不是數字):
// Boolean flag used to determine when a character other than a number is entered.
private bool nonNumberEntered = false;
// Handle the KeyDown event to determine the type of character entered into the control.
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
// Initialize the flag to false.
nonNumberEntered = false;
// Determine whether the keystroke is a number from the top of the keyboard.
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
// Determine whether the keystroke is a number from the keypad.
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
// Determine whether the keystroke is a backspace.
if(e.KeyCode != Keys.Back)
{
// A non-numerical keystroke was pressed.
// Set the flag to true and evaluate in KeyPress event.
nonNumberEntered = true;
}
}
}
//If shift key was pressed, it's not a number.
if (Control.ModifierKeys == Keys.Shift) {
nonNumberEntered = true;
}
}
// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Check for the flag being set in the KeyDown event.
if (nonNumberEntered == true)
{
// Stop the character from being entered into the control since it is non-numerical.
e.Handled = true;
}
}
// This is to allow only numbers.
// This Event Trigger, When key press event occures ,and it only allows the Number and Controls.,
private void txtEmpExp_KeyPress(object sender, KeyPressEventArgs e)
{
if(Char.IsControl(e.KeyChar)!=true&&Char.IsNumber(e.KeyChar)==false)
{
e.Handled = true;
}
}
//At key press event it will allows only the Characters and Controls.
private void txtEmpLocation_KeyPress(object sender, KeyPressEventArgs e)
{
if (Char.IsControl(e.KeyChar) != true && Char.IsNumber(e.KeyChar) == true)
{
e.Handled = true;
}
}
此代碼將區分字母字符鍵和非字母鍵:
private void maskedTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (Regex.IsMatch(e.KeyChar.ToString(), @"\p{L}"))
{
// this is a letter
}
else
{
// this is NOT a letter
}
}
更新:請注意,上述正則表達式模式將僅匹配字母字符,因此不允許使用空格,逗號,點等。 為了允許更多種類的字符,您需要將它們添加到模式中:
// allow alphabetic characters, dots, commas, semicolon, colon
// and whitespace characters
if (Regex.IsMatch(e.KeyChar.ToString(), @"[\p{L}\.,;:\s]"))
//添加文本框選擇它並轉到事件&在事件列表中雙擊“按鍵”事件。
if (!char.IsLetter(e.KeyChar))
{
MessageBox.Show("Enter only characters");
}
}
這對我有用:)
private void txt_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !((e.KeyChar != 'ñ' && e.KeyChar != 'Ñ') && char.IsLetter(e.KeyChar));
}
試試這個代碼
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.