force a combobox to accept numeric only

CharlieChau

Active member
Joined
Jun 15, 2004
Messages
26
Programming Experience
Beginner
hi,
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.
thanks,
/CC.
 

CharlieChau

Active member
Joined
Jun 15, 2004
Messages
26
Programming Experience
Beginner
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.
 

bloukewer

Well-known member
Joined
Jul 2, 2004
Messages
88
Programming Experience
3-5
Solution

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
messageBox.show("Please enter eight or less numeric characters only")
messagebox.focus()
endif

Hope this helps 'cause I did it out the top of my head.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,335
Location
Sydney, Australia
Programming Experience
10+
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.
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
did you noted this a bit hiden functionalities ... for instance:

VB.NET:
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 ;)
 
Top Bottom