How to get BackSpace - with the restriction of only numbers in the text field?

I am inserting this into an event KeyPress:

e.Handled = !Char.IsNumber(e.KeyChar);

But I do not have a key Backspace, how to fix it?

+5
source share
3 answers

What about:

e.Handled = !(Char.IsNumber(e.KeyChar) || e.KeyChar == 8);

Or equivalently:

e.Handled = !Char.IsNumber(e.KeyChar) && e.KeyChar != 8;

(As in the Roman answer , you can use '\b'instead of 8 in the code above.)

+16
source

here's how to check if backspace has been pressed:

if(e.KeyChar == '\b'){//backspace was pressed}
+7
source

backspace key
e.KeyChar == (char) Keys.Back

+2

All Articles