473,386 Members | 1,602 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,386 software developers and data experts.

Converting TextBox to number only

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
Nov 16 '05 #1
5 21458
Brian Robbins <Br***********@RavenInd.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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
"Brian Robbins" <Br***********@RavenInd.com> wrote in message
news:16*****************************@phx.gbl...
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

Nov 16 '05 #3
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.
Nov 16 '05 #4
Brian Robbins wrote:
[...]
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.
Nov 16 '05 #5
See my response to this. That is the solution

JIM
"Trina D. Martinez" <Tr************@discussions.microsoft.com> wrote in
message news:02**********************************@microsof t.com...
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?
"Brian Robbins" wrote:
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

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: John MacDonald | last post by:
I need to convert a string to a integer. I tryed to use the Val() Function but the i need the string to be added. The string is "(x+3)^2" where the x is a part of the for/next statement. And i used...
2
by: anonieko | last post by:
This applies to javascript dynamic textbox onkey > > > Newsgroups: comp.lang.javascript From: Lasse Reichstein Nielsen <l...@hotpop.com> - Find messages by this author Date: Fri, 15 Jul 2005...
0
by: PeterZ | last post by:
Hi, I have a win forms project, for simplicity's sake, lets say that I have a dataset and a textbox that is bound to a numeric column in the dataset. I also specified a ConvertEventHandler to...
3
by: Homer Simpson | last post by:
How do I convert a value in a textbox to a real number so I can perform some math ops on it? I understand I will need to validate the textbox's input to make sure it is a real number and not a...
4
by: Clark Stevens | last post by:
I have a program that I'm converting from VB6 to VB.NET. It reads in a text file containing barcode numbers and their corresponding descriptions. Then the user enters the barcode number and the...
3
by: RG | last post by:
I am reading in from a serial port a 16 bit number from 0 to 65536. It is being read as a string from my microcontroller into VB using DEC5 or 5 digits are displayed. I need to display this value...
1
by: RG | last post by:
I am reading from my serial port a 24 bit hex number which consists of 6 place values. For example: 7FED6F which equals 8383855 using the hex2dec excel function. I need to convert this hex into a...
1
missinglinq
by: missinglinq | last post by:
From the Access Object Dialog Box click on "Modules" Click on new to make a new module Copy then paste the following code into the new module: Function SayNo(ByVal N As Currency) As String ...
25
by: Blasting Cap | last post by:
I keep getting errors that pop up when I am trying to convert an application from dotnet framework 1.1 to framework 2.0. The old project was saved in sourcesafe from Visual Studio 2003, and I have...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.