2009-04-26 61 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); 
    } 
相关问题