我在 KeyPress
事件中插入这个:
e.Handled = !Char.IsNumber(e.KeyChar);
但是我没有Backspace键,如何解决?
最佳答案
怎么样:
e.Handled = !(Char.IsNumber(e.KeyChar) || e.KeyChar == 8);
或等效地:
e.Handled = !Char.IsNumber(e.KeyChar) && e.KeyChar != 8;
(与 roman's answer 一样,您也可以在上面的代码中使用 '\b'
而不是 8。)
关于c# - 如何获得退格键 - 文本框中只有数字限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1523895/