//textBox 的KeyPress 事件
private void txtID_KeyPress(object sender, KeyPressEventArgs e)
{//e.KeyChar!=8,意思是按住的键不为退格键,Char.IsDigit(e.KeyChar),意思是按的键盘不是0-9的数字
if (e.KeyChar!=8 && !Char.IsDigit(e.KeyChar))
{
MessageBox.Show("只能输入数字!");
//e.handed=true,意思就是你按了一个错键,系统认为你没有按
e.Handled = true;
}
}
效果如图
如果,e.Handled=false,效果如下:
Messagebox 确定按钮以后,按错的a 会显示在TextBox中