force a combobox to accept numeric only


Active member
Jun 15, 2004
Programming Experience
how can I force a combobox in VB .NET to accept the 8 digits only, It can not be more or less and it has to be a number.
It is me who posted the question. Since I use the tablet PC, so you can write on the combobox, even I already put a list of the number in the combobox, but if you type or using the pen to enter the information. The input will accept the characters.

One way of doing that would be to double-click the combobox. The "SelectedIndexChanged"-eventhandler will appear before you. In the right-hand corner of your screen you'll see a dropdownbox with the text: "SelectedInexChanged"). Click on the box and choose "TextChanged". The "TextChanged" eventhandler will now appear before you.
Now enter the following code (this is pseudo-code and you'll have to adjust it):

if not isNumeric(Combobox1.text) then"Please enter eight or less numeric characters only")

Hope this helps 'cause I did it out the top of my head.
If you want to prevent the user from entering anything into a ComboBox that is not in the drop-down list, set the DropDownStyle to DropDownList instead of DropDown.
did you noted this a bit hiden functionalities ... for instance:

Private Shared Function IsCharacterAllowed(ByVal c As Char) As Boolean 
		If Char.IsControl(c) Then Return False 
		If Char.IsDigit(c) Then Return True 
		If Char.IsLetter(c) Then Return False 'Letters forbiden 
		If Char.IsSeparator(c) Then Return False 
		If Char.IsWhiteSpace(c) Then Return False 
		If Char.IsSymbol(c) Then Return False 
		If Char.IsPunctuation(c) Then Return True 'Comma is allowed 
	End Function

Cheers ;)

Latest posts