473,327 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 30627
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dennis Craven | last post by:
Hello, Does anyone know how one could insert a backspace into a widget such as a TextView/Buffer in Pygtk? Whenever I try something like insert(iter, '\b') I get jibberish inserted into the...
1
by: Roderick Thomas | last post by:
This problem centers around the basic character/line count problem most people have asked and have received numerous responses to. However, my question is a little more detailed and I'm hoping...
1
by: haxmya | last post by:
Ok, made an explorer toolbar (band Object) with a textbox on it. Everything works great....except that the backspace button doesn't actually backspace when you're in the textbox. Shift-backspace...
6
by: Robert Nurse | last post by:
Hi, Is it at all possible to capture and suppress the backspace key when focus is not on a form edit control? Basically, while the page is loading I don't want the user pressing the backspace...
17
by: Julia Briggs | last post by:
Are there any gotchas using if (event.keyCode==8)? I understand that to represent backspace, but it doesn't work. I am running Windows XP, using a typical keyboard - but no luck in detecting...
3
by: Dave Christian | last post by:
What event gets fire off? I thought it might be an onLoad event, but that doesn't seem to be the case. (I have some variables displayed on my page that, upon a Backspace (or even hitting the...
7
by: Lars Netzel | last post by:
Hi How do I in a textbox only allow users to type in numbers and comma and dot ? they should of course be able to delete and backspace the stuff they type in too.. I figured keyDown would...
5
by: PJ | last post by:
I've been using this function to limit a text input to numbers only with success on an old site. function checkForInt(evt) { var charCode = ( evt.which ) ? evt.which : event.keyCode; return (...
1
by: Blue | last post by:
This JS limits the input characters into the form. How do I modify it so that it also allows CARRIAGE RETURN and BACKSPACE (for making text correction)? Due to the template engine I am using, I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.