Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get BackSpace - with only numbers limit in textbox?

Tags:

c#

winforms

I insert this on KeyPress event:

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

But I don't have the Backspace key, how to fix it?

like image 480
Gold Avatar asked Oct 06 '09 06:10

Gold


2 Answers

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.)

like image 53
Jon Skeet Avatar answered Sep 25 '22 14:09

Jon Skeet


here's how to check if backspace was pressed:

if(e.KeyChar == '\b'){//backspace was pressed}
like image 21
roman m Avatar answered Sep 23 '22 14:09

roman m