PC Review


Reply
Thread Tools Rate Thread

Allow backspace

 
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      26th Sep 2005

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;
}

 
Reply With Quote
 
 
 
 
Peter Rilling
Guest
Posts: n/a
 
      26th Sep 2005
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" <(E-Mail Removed)> wrote in message
news:F5BFB140-0A1F-428A-8A0D-(E-Mail Removed)...
>
> 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;
> }
>



 
Reply With Quote
 
Mythran
Guest
Posts: n/a
 
      27th Sep 2005

"Mike L" <(E-Mail Removed)> wrote in message
news:F5BFB140-0A1F-428A-8A0D-(E-Mail Removed)...
>
> 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

 
Reply With Quote
 
Morten Wennevik
Guest
Posts: n/a
 
      27th Sep 2005
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 <(E-Mail Removed)> 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]
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
backspace key Mark Barnard Microsoft Excel Programming 3 10th Oct 2008 10:46 PM
Backspace Key Caster Keller Microsoft Word Document Management 2 10th Oct 2008 10:31 AM
backspace key MOE DESIGN Microsoft Word Document Management 1 28th Jul 2008 09:40 AM
backspace key MOE DESIGN Microsoft Word Document Management 1 23rd Jul 2008 05:41 PM
Backspace key Need Help Windows XP Internet Explorer 1 26th Oct 2003 11:08 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:24 AM.