A combobox control in VB.Net can have three possible styles - Simple, Dropdown or DropDownList.
A simple combobox will have the list always visible and user can enter new values that are not in the list.
In DropDownList style, users cannot enter new values. Only values that are already in the list can be selected by clicking the down arrow.
With DropDown style, which is the default style, users can enter new values or select an item from the list by clicking the down arrow.
You can easily change the style from the Properties window, DropDownStyle property or at runtime.
The figure below shows the three different combobox styles
Restricting the text entered on a DropDown style Combobox
On a DropDown style combobox, users can enter any value in the input area of the combobox. If you do not want users to enter values that are not in the list, you could do so with this nice little trick !!
Add the following piece of code to the TextUpdate Event of your combobox
If ComboBox1.FindString(ComboBox1.Text) < 0 Then ComboBox1.Text = ComboBox1.Text.Remove(ComboBox1.Text.Length - 1) ComboBox1.SelectionStart = ComboBox1.Text.Length End If
The TextUpdate Event is fired every time user enters some text in the comboxbox.
The first line of this code tries to find the an item in the combobox list that starts with the input text.
If no match is found then a negative value is returned. In such case you remove the last character that is input. That is what the second line of the code does.
Finally you want to keep the cursor at the end of the input because the remove function on second line move the cursor to the beginning of the input text.
To make the input easier for the user it is always a good technique to also set the AutoCompleteMode to SuggestAppend and AutoCompleteSource to ListItems
ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems