A Textbox ActiveX Control That Limits Input to Numbers

Martin LissKeep everyone healthy; Get Vaccinated
CERTIFIED EXPERT
Almost 50 years of programming experience. Click '+ More' in my "Full Biography" to see links to some articles I've written.
Published:
Updated:
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone to do so.

The attached control is a modified TextBox does all that for you and it also has these additional properties:

CanHaveDecimals - This is a Boolean property that determines if the number can have decimals or not

CanBeNegative - This is a Boolean property that determines if the number can be negative or not

MaxDecimals - This property allows the user to set the maximum allowable number of decimals

MinValue - This property allows the user to set the minimum acceptable value

MaxValue - This property allows the user to set the maximum acceptable value

DecimalSeparator - This property determines which character ("." or ",") is used as the decimal separator

RequireLeadingDigit - This is a Boolean property that determines if at least one digit must preceed the decimal separator

Here is a link to the code which once compiled will add the NumberBox to the list of available project Components. You can add the control to a project by going to Project|Components and checking NumberBox. I would have liked to be able to attach the code here in a zip file, but the zip contains file types that Experts Excgange won't allow to be uploaded even though they are all perfectly valid VB file types.

Note that this control is fairly widely used (for a 3rd-party OCX) on another site and has gone through 16 versions with associated version testing so it is thoroughly tested. Having said that if you do find a problem please let me know the details and I'll attempt to correct it.
 
If you find that this article has been helpful, please click the “thumb’s up” button below. Doing so lets me know what is valuable for EE members and provides direction for future articles. It also provides me with positive feedback in the form of a few points. Thanks!
2
8,269 Views
Martin LissKeep everyone healthy; Get Vaccinated
CERTIFIED EXPERT
Almost 50 years of programming experience. Click '+ More' in my "Full Biography" to see links to some articles I've written.

Comments (2)

CERTIFIED EXPERT

Commented:
why not use a maskedbox that is already there in VB
Martin LissKeep everyone healthy; Get Vaccinated
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2023

Author

Commented:
Yes you could, but I find them awkward to use and they don't have all the features that my control has. If you'd like to discuss this further then please send me an EE message.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.