Converting TextBox to number only

  • Thread starter Thread starter Brian Robbins
  • Start date Start date
B

Brian Robbins

In standard C/C++ SDK, MFC, or VCL if I wanted to make a
TextBox (CEditBox, TEditBox, etc.) only except numbers I
have dozens of ways to handle it. But none of those
methods are working in C#.NET. I may be just having a
paradigm shift issue and I just don't understand new the
methods fully, yet.

I would love to use the Validation methods, but they occur
WAY TOO LATE in the event order list! I don't want the
TextBox.Text to ever have an incorrect character in the
string. For example, if the user types an 'A' it should
never be added to the TextBox.Text. In C++ I would have
used the KeyDown event to filter values that aren't
numbers. In C# this appears impossible. The e.Key,
e.KeyData, etc. are readonly so I can't set them to null
(like I would have done in C++). The e.Handled property
don't seem to do anything of value.

So, fellow geniuses, please help me see what I am
overlooking.


Thanks,
Brian
 
Brian Robbins said:
In standard C/C++ SDK, MFC, or VCL if I wanted to make a
TextBox (CEditBox, TEditBox, etc.) only except numbers I
have dozens of ways to handle it. But none of those
methods are working in C#.NET. I may be just having a
paradigm shift issue and I just don't understand new the
methods fully, yet.

I would love to use the Validation methods, but they occur
WAY TOO LATE in the event order list! I don't want the
TextBox.Text to ever have an incorrect character in the
string. For example, if the user types an 'A' it should
never be added to the TextBox.Text. In C++ I would have
used the KeyDown event to filter values that aren't
numbers. In C# this appears impossible. The e.Key,
e.KeyData, etc. are readonly so I can't set them to null
(like I would have done in C++). The e.Handled property
don't seem to do anything of value.

Yes it does - setting e.Handled to true means the event isn't used for
anything else. However, I believe you need to use the KeyPress event,
not KeyDown. I've used KeyPress event handlers like this:

if (e.KeyChar > 31 && (e.KeyChar < '0' || e.KeyChar > '9'))
{
e.Handled=true;
}

to handle numeric-only fields.
 
You can also sub-class the control with your own, and handle the OnKeyDown
or OnKeyPress event and then just do not call the base calss implementation.
I prefer inheriting my own specialized sub-classes for this type of
situation. You could call it NumericTextBox that way you can then stick it
on your toolbox and drop it in when you want it

JIM
 
Thanks, Jon. The following combination you provided
worked great!

.... KeyPress...
{
if (e.KeyChar < '0' || e.KeyChar > '9')
{
e.Handled=true;
}
}
Yes it does - setting e.Handled to true means the event
isn't used for anything else. However, I believe you
need to use the KeyPress event, not KeyDown. I've used
KeyPress event handlers like this:

This code SHOULD work the same way. But it don't...

.... KeyDown...
{
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
e.Handled=true;
}
}

This is what I had before beforing posting here. I set a
breakpoint on both pieces of code (for different
TextBoxes). They appear to operate the same, but the
KeyDown doesn't produce the same results. It does in MS
SDK, MS MFC, and Borland's VCL. In C#.NET it doesn't, so
I assumed it had to with e.Handled part. Maybe not...
but, thanks anyway.
 
Brian said:
[...]
This code SHOULD work the same way. But it don't...

... KeyDown...
{
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
e.Handled=true;
}
}
[...]

As I recall, setting Handled in KeyUp/KeyDown just doesn't work -- you
have to use KeyPress.
 
See my response to this. That is the solution

JIM


Trina D. Martinez said:
Hello: I am having the exact same problem. Did this get resolved. I
have several dozen numeric fields where the value of null needs to be valid.
Typically the user will clear the field and move on. This is causing the
value to change back to the original value. The user can change it to a
different number or 0. Zero will not work for this environment, it needs to
be null. Any ideas?
 
Back
Top