Click here to Skip to main content
15,897,371 members
Articles / Programming Languages / Visual Basic
Article

Numeric TextBox

Rate me:
Please Sign up or sign in to vote.
3.33/5 (30 votes)
1 Apr 20062 min read 202.1K   5.4K   51   35
The article disucusses a new way to let a TextBox only accept numeric values, without using custom controls.

Sample Image - NumericTextBox.gif

Introduction

Many of us have faced a situation where we want the user to enter a number (an integer or a double) in a TextBox, thus seeking a way to prevent him from inserting invalid characters. Almost all of the available solutions required to use a custom control inherited from TextBox control. Regarding the fact that the developers sometimes (or perhaps often!) decide to add this facility to their TextBoxes after they have added several TextBox controls, and added many other lines of code to their project based on these controls names, these solutions are practically of limited use. (The programmer needs to remove all TextBoxes, add the new custom control instead of each of them, and make all the required changes to the code.)

Considering that, I broke the project to two blocks:

  1. To validate each character a user presses on the keyboard, check if its addition to the current value of the TextBox will still result in a numeric value, and prevent the character to be added to the TextBox value if the answer is no.
  2. To perform the validation on pasting into the TextBox. Pasting could be done through pressing CTRL+V, SHIFT+INSERT or using the right-click context menu.

The first issue can be handled using the onKeyPress event of the TextBox. For the second part, unfortunately, we don't have an onPaste event. I used a brilliant method dedicated in this Experts-Exchange thread.

Using the code

You will need to set the numericTextboxKeyPress sub to handle the onKeyPress event for all TextBoxes you want to validate. This will handle the input characters as the user types them.

The creative TextBoxOnChange class handles the paste event for the TextBoxes. For each TextBox on which you want to handle the paste event, you should add a line of code like this:

VB
//Example of adding an onPaste handler
Dim onPaste1 As New TextBoxOnPaste(Me.TextBox1)

I added two checkboxes to the demo project source code to facilitate your learning how to use the code for multiple checkboxes.

History

25 Feb 2006 : Minor bug fixes.

19 Feb 2006 : Original article submitted.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNumeric TextBox in VB.Net Pin
SumitSaha11-Jan-15 15:42
SumitSaha11-Jan-15 15:42 
GeneralMy vote of 5 Pin
Pawan.riseup3-Jun-14 22:34
Pawan.riseup3-Jun-14 22:34 
GeneralMy vote of 1 Pin
Kazna4ey8-Jun-13 6:51
Kazna4ey8-Jun-13 6:51 
QuestionAdvanced Numeric Pin
Sureshjv11-Dec-12 19:54
Sureshjv11-Dec-12 19:54 
QuestionHey You can check here Advanced Numeric Text Box http://getvbnetcode.blogspot.in/ Pin
Sureshjv11-Dec-12 19:53
Sureshjv11-Dec-12 19:53 
GeneralPasted data Pin
iamthecodecutter8-May-11 19:26
iamthecodecutter8-May-11 19:26 
GeneralMy vote of 1 Pin
saeed mousavi4-Feb-09 4:54
saeed mousavi4-Feb-09 4:54 
GeneralCool and what about positive numbers Pin
SalarSoft11-Jul-07 20:07
SalarSoft11-Jul-07 20:07 
Generaldecimal point Pin
torbero11-Jul-07 8:30
torbero11-Jul-07 8:30 
GeneralRe: decimal point Pin
Hojjat Salmasian12-Jul-07 4:16
Hojjat Salmasian12-Jul-07 4:16 
GeneralSmall Issue Pin
MNC6217-Jun-07 21:26
MNC6217-Jun-07 21:26 
GeneralFlaw.... Pin
Christopher Stratmann9-Feb-07 7:31
Christopher Stratmann9-Feb-07 7:31 
GeneralRe: Flaw.... Pin
Hojjat Salmasian9-Feb-07 9:58
Hojjat Salmasian9-Feb-07 9:58 
GeneralThank you Pin
PocketJem5-Dec-06 9:49
PocketJem5-Dec-06 9:49 
GeneralRe: Thank you Pin
Hojjat Salmasian5-Dec-06 19:06
Hojjat Salmasian5-Dec-06 19:06 
GeneralRe: Thank you Pin
PocketJem6-Dec-06 8:35
PocketJem6-Dec-06 8:35 
GeneralRe: Thank you Pin
Hojjat Salmasian7-Dec-06 6:58
Hojjat Salmasian7-Dec-06 6:58 
GeneralThe contorl don't support inputing minus symbol. Pin
Zhaoxp22-Feb-06 20:46
Zhaoxp22-Feb-06 20:46 
GeneralRe: The contorl don't support inputing minus symbol. Pin
Hojjat Salmasian23-Feb-06 4:35
Hojjat Salmasian23-Feb-06 4:35 
GeneralRe: The contorl don't support inputing minus symbol. Pin
Ray Cassick25-Feb-06 11:22
Ray Cassick25-Feb-06 11:22 
GeneralRe: The contorl don't support inputing minus symbol. Pin
Hojjat Salmasian26-Feb-06 6:31
Hojjat Salmasian26-Feb-06 6:31 
GeneralRe: The contorl don't support inputing minus symbol. Pin
Ray Cassick26-Feb-06 7:12
Ray Cassick26-Feb-06 7:12 
If the aim of your text box is to allow only numeric values only then you have to consider exactly what IS a number.

For example:

$4.00 is a number
50% is a number
2 is a number
5:00pm could also be considered a number

All these above examples are 'numbers' because they represent a numeric AMOUNT type of a construct.

The example that the other person gave of '123-456-789' really does not represent a numeric amount, it represents a pattern of characters that just in this case happen to all be characters that are allowed in numeric display.

Maybe I am over thinking this a bit, and if I am I am sorry to muddy the discussion here, but I think if your controls focus is to only allow numeric values then you have to consider what really IS a numeric value. Not just the individual characters but what they represent as a whole when entered in. This has a larger meaning than just creating a control that only allows numeric type characters in.

If you are only allowing numeric type characters then you have to take into account ALL the different numeric characters and I am not that sure of the overall usefulness of the control at that point.

As one further example... IPV4 addresses allow for 4 groups of numbers, each separated by a '.'. Each characters is considered a numeric type character but as whole and IP address is not considered a numeric value. Should your control allow and validate this as a proper value? If it does then it has moved from being a numeric value text box into the realm of an IP address text box.

Just trying to push home the fact that once you widen the scope of your control very wide then you open yourself up to tons of problems. If a person wants to enter in an IP address then they should use a control specifically for that type of entry. If a person wants to enter a numeric value (dollars, pounds, percents, etc...) then they use yours here. If they want to end in something else then maybe they use another. Just trying to say that you need to keep your scope on point here and be ready and willing to tell people requesting features and pointing out what 'they' think are bugs, that their request doe snot fit the scope of your control and for what reasons you feel that way. This is one reason why there are so many controls out there that end up no good. They try to be all things to all people and in the end never do anything very well.




My Blog[^]
FFRF[^]



-- modified at 13:19 Sunday 26th February, 2006
GeneralRe: The contorl don't support inputing minus symbol. Pin
Hojjat Salmasian27-Feb-06 6:46
Hojjat Salmasian27-Feb-06 6:46 
GeneralRe: The contorl don't support inputing minus symbol. Pin
LovinBill12-Jul-06 23:56
LovinBill12-Jul-06 23:56 
GeneralRe: The contorl don't support inputing minus symbol. Pin
Hojjat Salmasian15-Jul-06 4:06
Hojjat Salmasian15-Jul-06 4:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.