正在验证TextBox仅接受字母/小写字符
本文关键字:字符 验证 TextBox | 更新日期: 2023-09-27 18:21:30
我有用于验证仅接受字母/小写字符的text_box的代码。我已经设法在下面的代码中添加了代码。
这是我的代码:
if (char.IsLetter(e.KeyChar) || e.KeyChar == 8)
{
e.handled = false;
}
else
{
e.handled = true;
}
尝试下面的代码
public Form2()
{
InitializeComponent();
TextBox1.CharacterCasing = CharacterCasing.Lower;
}
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !char.IsLetter(e.KeyChar);
}
基本上我在这里做的是
- 在构造函数中,我将文本框设置为将键入的任何字符转换为小写
- 在文本框中添加KeyPress事件,并检查键入的字符不是字母或字符8