Click here to Skip to main content
Click here to Skip to main content

Numeric TextBox

By , 1 Apr 2006
 

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:

//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

About the Author

Hojjat Salmasian
Web Developer
United States United States
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberKazna4ey8-Jun-13 6:51 
QuestionAdvanced NumericmemberSureshjv11-Dec-12 19:54 
QuestionHey You can check here Advanced Numeric Text Box http://getvbnetcode.blogspot.in/memberSureshjv11-Dec-12 19:53 
GeneralPasted datamemberiamthecodecutter8-May-11 19:26 
GeneralMy vote of 1membersaeed mousavi4-Feb-09 4:54 
GeneralCool and what about positive numbersmemberSalarSoft11-Jul-07 20:07 
Generaldecimal pointmembertorbero11-Jul-07 8:30 
GeneralRe: decimal pointmemberHojjat Salmasian12-Jul-07 4:16 
GeneralSmall IssuememberMNC6217-Jun-07 21:26 
GeneralFlaw....memberchris1759-Feb-07 7:31 
GeneralRe: Flaw....memberHojjat Salmasian9-Feb-07 9:58 
GeneralThank youmemberPocketJem5-Dec-06 9:49 
GeneralRe: Thank youmemberHojjat Salmasian5-Dec-06 19:06 
Thanks for the complements. I'm glad I've helped you.
 
Hojjat
GeneralRe: Thank youmemberPocketJem6-Dec-06 8:35 
GeneralRe: Thank youmemberHojjat Salmasian7-Dec-06 6:58 
GeneralThe contorl don't support inputing minus symbol.memberzxp31922-Feb-06 20:46 
GeneralRe: The contorl don't support inputing minus symbol.memberHojjat Salmasian23-Feb-06 4:35 
GeneralRe: The contorl don't support inputing minus symbol.memberRay Cassick25-Feb-06 11:22 
GeneralRe: The contorl don't support inputing minus symbol.memberHojjat Salmasian26-Feb-06 6:31 
GeneralRe: The contorl don't support inputing minus symbol.memberRay Cassick26-Feb-06 7:12 
GeneralRe: The contorl don't support inputing minus symbol.memberHojjat Salmasian27-Feb-06 6:46 
GeneralRe: The contorl don't support inputing minus symbol.memberLovinBill12-Jul-06 23:56 
GeneralRe: The contorl don't support inputing minus symbol.memberHojjat Salmasian15-Jul-06 4:06 
Questionvs2005 only?memberGaryMinor222-Feb-06 10:31 
AnswerRe: vs2005 only?memberChristian Graus22-Feb-06 10:54 
AnswerRe: vs2005 only?memberHojjat Salmasian22-Feb-06 16:08 
Questionwhat about coma ?memberscalpa9822-Feb-06 0:28 
AnswerRe: what about coma ?memberHojjat Salmasian22-Feb-06 5:40 
GeneralRe: what about coma ?memberscalpa9822-Feb-06 6:48 
GeneralRe: what about coma ?memberHojjat Salmasian22-Feb-06 16:04 
GeneralRe: what about coma ?memberscalpa9822-Feb-06 19:18 
GeneralRe: what about coma ?memberHojjat Salmasian23-Feb-06 4:33 
AnswerRe: what about coma ?memberRob_III4-Sep-06 5:19 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 1 Apr 2006
Article Copyright 2006 by Hojjat Salmasian
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid