I insert this on KeyPress
event:
e.Handled = !Char.IsNumber(e.KeyChar);
But I don't have the Backspace key, how to fix it?
How about:
e.Handled = !(Char.IsNumber(e.KeyChar) || e.KeyChar == 8);
Or equivalently:
e.Handled = !Char.IsNumber(e.KeyChar) && e.KeyChar != 8;
(As in roman's answer, you can use '\b'
instead of 8 in the above code too.)
here's how to check if backspace was pressed:
if(e.KeyChar == '\b'){//backspace was pressed}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With