2009-04-26 62 views
3

我有,我想只有在接受字母值的maskedtextbox控制Windows窗體應用程序。如何使文本框只接受字母字符

理想的情況下,這會表現得這樣按其它鍵比字母鍵會要麼不產生任何結果,要麼立即向用戶提供關於無效字符的反饋。

回答

4

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; 
    } 
} 
3

此代碼將區分字母按鍵從非字母的:

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 
    } 
} 

更新:請注意,上述正則表達式將只匹配字母字符,所以它不會允許SPAC es,逗號,點等。爲了讓更多類型的字符,你需要將這些添加到模式:

// allow alphabetic characters, dots, commas, semicolon, colon 
// and whitespace characters 
if (Regex.IsMatch(e.KeyChar.ToString(), @"[\p{L}\.,;:\s]")) 
6

這個問題可能已經問及對每一個可以想象的編程論壇回答了百萬次。所提供的每一個答案都有一個獨特於所述要求的區別。

由於您使用的是MaskedTextBox,因此您可以使用其他驗證功能,並且不需要處理按鍵。您可以簡單地將Mask屬性設置爲「L」(需要輸入字符)或「?」 (可選字符)。爲了向用戶顯示輸入不可接受的反饋,可以使用BeepOnError屬性或添加工具提示來顯示錯誤消息。這個反饋機制應該在MaskedInputRejected事件處理程序中執行。

MaskedTextBox控件提供ValidatingType屬性來檢查通過掩碼要求的輸入,但可能不是正確的數據類型。在此類型驗證之後引發TypeValidationCompleted事件,您可以處理它以確定結果。

如果您仍然需要處理按鍵事件,請繼續閱讀...!

我推薦你的方法是,不是處理KeyDown事件(你表面上不需要高級密鑰處理能力),或者使用正則表達式來匹配輸入(坦率地說,過度殺傷),我會簡單地使用內置的-in屬性的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; 
    } 
} 

請注意,此代碼段允許您處理punctutation和分隔符鍵。

3
// 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; 
    } 
} 
+0

可能要修復您的格式。 – 2011-07-11 17:34:30

1

//添加一個文本框選擇它&轉到活動&在的「按鍵」事件的事件列表中雙擊。

 if (!char.IsLetter(e.KeyChar)) 
     { 
      MessageBox.Show("Enter only characters"); 
     } 
    } 
0

這對我的作品:)

private void txt_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     e.Handled = !((e.KeyChar != 'ñ' && e.KeyChar != 'Ñ') && char.IsLetter(e.KeyChar)); 
    } 
0

嘗試this代碼

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space); 
    } 
相關問題