首先添加下面代码

//禁止在textBox输入框输入非法字符
private void keypressed(Object o, KeyPressEventArgs e)
{ 
if (e.KeyChar != '\b')//这是允许输入退格键 
{
if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字 
{
e.Handled = true;
}
}
}

然后给textBox输入框添加KeyPress事件

 

C# 禁止在textBox输入框输入非法字符

 

相关文章: