我想限制用户在文本框中仅输入数字。
我在按键事件中添加此代码:
private void txtPartID_KeyPress(object sender, KeyPressEventArgs e)
{
if (((e.KeyChar >= '0') && (e.KeyChar <= '9')) == false)
{
e.Handled = true;
}
}
但之后 BackSpace 键对此文本框不起作用。我怎样才能改变这个?
您可以使用它检查退格键,
if(e.KeyChar == '\b')
仅检查数字的更好方法是
private void txtPartID_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !(Char.IsNumber(e.KeyChar) || e.KeyChar == 8);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)