代码如下:
调用TextBox的KeyPress事件
private void txtUserId_KeyPress(object sender, KeyPressEventArgs e)
{
//如果输入的不是数字键,也不是回车键、Backspace键,则取消该输入
if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar!=(char)13 &物巧斗& e.KeyChar!=(char)8)
{
e.Handled = true;
}
}
注意事宽饥项
C#文本框输入限制
//只能输入数字和小数点和退格键
private void txt_KeyPress(object sender, KeyPressEventArgs e)
{
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
{
e.Handled = true;
}
}
//只能输入数字和退格键
private void txt_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
{
e.Handled = true;
}
}
//限制输入只能为数字
private void txt_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (Char)8)
{
e.Handled = true;
}
}
//限制输入不能为中文和全角
private void txt_KeyPress(object sender, KeyPressEventArgs e)
{
int chfrom = Convert.ToInt32("4e00", 16); //范围(0x4e00~0x9fa5)转换成int(chfrom~chend)
int chend = Convert.ToInt32("9fa5", 16);
if (e.KeyChar >= (Char)chfrom && e.KeyChar <= (Char)chend)
{
e.Handled = true;
}
if (e.KeyChar >= (Char)65281 & (int)e.KeyChar <= (Char)65374)
{
e.Handled = true;
}
}
//限制输入只能输入数字和字母,退格键
private void txt_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z')
|| (e.KeyChar >= '罩磨0' && e.KeyChar <= '9') || (e.KeyChar == 8))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}