【发布时间】:2010-10-21 21:08:56
【问题描述】:
我有一个带有 maskedtextbox 控件的 Windows 窗体应用程序,我只想接受字母值。
理想情况下,按字母键以外的任何其他键都不会产生任何结果或立即向用户提供有关无效字符的反馈。
【问题讨论】:
我有一个带有 maskedtextbox 控件的 Windows 窗体应用程序,我只想接受字母值。
理想情况下,按字母键以外的任何其他键都不会产生任何结果或立即向用户提供有关无效字符的反馈。
【问题讨论】:
这个问题可能已经在每个可以想象的编程论坛上被问过并回答了一百万次。提供的每个答案都具有对所述要求的独特性。
由于您使用的是MaskedTextBox,因此您可以使用额外的验证功能,并且不需要处理按键操作。您可以简单地将 Mask 属性设置为“L”(需要字符)或“?” (可选字符)。为了向用户显示输入不可接受的反馈,您可以使用BeepOnError 属性或添加工具提示来显示错误消息。这种反馈机制应该在MaskedInputRejected 事件处理程序中实现。
MaskedTextBox 控件提供了一个ValidatingType 属性来检查输入是否通过了掩码的要求,但可能不是正确的数据类型。 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;
}
}
请注意,此 sn-p 还允许您处理标点符号和分隔符键。
【讨论】:
来自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;
}
}
【讨论】:
此代码将区分字母字符按键和非字母按键:
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]"))
【讨论】:
// 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;
}
}
【讨论】:
//添加一个文本框选择它&转到事件&在事件列表中双击“keypress”事件。
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));
}
【讨论】:
试试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);
}
【讨论】: