469,953 Members | 2,400 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,953 developers. It's quick & easy.

Allow backspace


In a text box I want to allow only numbers and backspace.

Currently the code I have only allows numbers. How do I also allow backspace?

if (!Char.IsDigit(e.KeyChar))
{
e.Handled = true;
}

Nov 17 '05 #1
3 30175
Put a breakpoint there and see what character code you get when you hit
backspace. It should be one of the control characters.

"Mike L" <Ca***@nospam.nospam> wrote in message
news:F5**********************************@microsof t.com...

In a text box I want to allow only numbers and backspace.

Currently the code I have only allows numbers. How do I also allow
backspace?

if (!Char.IsDigit(e.KeyChar))
{
e.Handled = true;
}

Nov 17 '05 #2

"Mike L" <Ca***@nospam.nospam> wrote in message
news:F5**********************************@microsof t.com...

In a text box I want to allow only numbers and backspace.

Currently the code I have only allows numbers. How do I also allow
backspace?

if (!Char.IsDigit(e.KeyChar))
{
e.Handled = true;
}


if (!Char.IsDigit(e.KeyChar) && e.KeyCode != Keys.Back) {
e.Handled = True;
}

HTH,
Mythran

Nov 17 '05 #3
Hi Mike,

I usually use Char.IsControl as this allows for cut and paste as well.

if (!Char.IsDigit(e.KeyChar) || !Char.IsControl(e.KeyChar))
e.Handled = true;
On Mon, 26 Sep 2005 23:33:03 +0200, Mike L <Ca***@nospam.nospam> wrote:

In a text box I want to allow only numbers and backspace.

Currently the code I have only allows numbers. How do I also allow backspace?

if (!Char.IsDigit(e.KeyChar))
{
e.Handled = true;
}


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Dennis Craven | last post: by
1 post views Thread by Roderick Thomas | last post: by
1 post views Thread by haxmya | last post: by
6 posts views Thread by Robert Nurse | last post: by
17 posts views Thread by Julia Briggs | last post: by
3 posts views Thread by Dave Christian | last post: by
5 posts views Thread by PJ | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.