Click here to Skip to main content
13,862,137 members
Click here to Skip to main content
Add your own
alternative version


50 bookmarked
Posted 19 Feb 2006

Numeric TextBox

, 1 Apr 2006
Rate this:
Please Sign up or sign in to vote.
The article disucusses a new way to let a TextBox only accept numeric values, without using custom controls.

Sample Image - NumericTextBox.gif


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.


25 Feb 2006 : Minor bug fixes.

19 Feb 2006 : Original article submitted.


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

You may also be interested in...

Comments and Discussions

QuestionNumeric TextBox in VB.Net Pin
SumitSaha11-Jan-15 16:42
memberSumitSaha11-Jan-15 16:42 
GeneralMy vote of 5 Pin
Pawan.riseup3-Jun-14 23:34
memberPawan.riseup3-Jun-14 23:34 
GeneralMy vote of 1 Pin
Kazna4ey8-Jun-13 7:51
memberKazna4ey8-Jun-13 7:51 
QuestionAdvanced Numeric Pin
Sureshjv11-Dec-12 20:54
memberSureshjv11-Dec-12 20:54 
QuestionHey You can check here Advanced Numeric Text Box Pin
Sureshjv11-Dec-12 20:53
memberSureshjv11-Dec-12 20:53 
GeneralPasted data Pin
iamthecodecutter8-May-11 20:26
memberiamthecodecutter8-May-11 20:26 
GeneralMy vote of 1 Pin
saeed mousavi4-Feb-09 5:54
membersaeed mousavi4-Feb-09 5:54 
GeneralCool and what about positive numbers Pin
SalarSoft11-Jul-07 21:07
memberSalarSoft11-Jul-07 21:07 
Generaldecimal point Pin
torbero11-Jul-07 9:30
membertorbero11-Jul-07 9:30 
GeneralRe: decimal point Pin
Hojjat Salmasian12-Jul-07 5:16
memberHojjat Salmasian12-Jul-07 5:16 
GeneralSmall Issue Pin
MNC6217-Jun-07 22:26
memberMNC6217-Jun-07 22:26 
GeneralFlaw.... Pin
Christopher Stratmann9-Feb-07 8:31
memberChristopher Stratmann9-Feb-07 8:31 
GeneralRe: Flaw.... Pin
Hojjat Salmasian9-Feb-07 10:58
memberHojjat Salmasian9-Feb-07 10:58 
GeneralThank you Pin
PocketJem5-Dec-06 10:49
memberPocketJem5-Dec-06 10:49 
GeneralRe: Thank you Pin
Hojjat Salmasian5-Dec-06 20:06
memberHojjat Salmasian5-Dec-06 20:06 
Thanks for the complements. I'm glad I've helped you.

GeneralRe: Thank you Pin
PocketJem6-Dec-06 9:35
memberPocketJem6-Dec-06 9:35 
GeneralRe: Thank you Pin
Hojjat Salmasian7-Dec-06 7:58
memberHojjat Salmasian7-Dec-06 7:58 
GeneralThe contorl don't support inputing minus symbol. Pin
Zhaoxp22-Feb-06 21:46
memberZhaoxp22-Feb-06 21:46 
GeneralRe: The contorl don't support inputing minus symbol. Pin
Hojjat Salmasian23-Feb-06 5:35
memberHojjat Salmasian23-Feb-06 5:35 
GeneralRe: The contorl don't support inputing minus symbol. Pin
Ray Cassick25-Feb-06 12:22
memberRay Cassick25-Feb-06 12:22 
GeneralRe: The contorl don't support inputing minus symbol. Pin
Hojjat Salmasian26-Feb-06 7:31
memberHojjat Salmasian26-Feb-06 7:31 
GeneralRe: The contorl don't support inputing minus symbol. Pin
Ray Cassick26-Feb-06 8:12
memberRay Cassick26-Feb-06 8:12 
GeneralRe: The contorl don't support inputing minus symbol. Pin
Hojjat Salmasian27-Feb-06 7:46
memberHojjat Salmasian27-Feb-06 7:46 
GeneralRe: The contorl don't support inputing minus symbol. Pin
LovinBill13-Jul-06 0:56
memberLovinBill13-Jul-06 0:56 
GeneralRe: The contorl don't support inputing minus symbol. Pin
Hojjat Salmasian15-Jul-06 5:06
memberHojjat Salmasian15-Jul-06 5: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.

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