By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

Allow backspace

P: n/a

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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a

"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

P: n/a
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.