You will need to populate the Items list with your values (manually or via data binding), and then set DropDownStyle to DropDownList
.
The combobox will not look like a text box, but if it has focus, entering it will automatically select the best match from the Items list.
(This is the recommended way to install Combobox so as not to allow custom text.)
Alternatively, if you want the style to be DropDown, grab the KeyPress
event of the control and do a quick check of the control text plus e.KeyChar
, and if it is not found in the list, set e.Handled = True
. This will block all keystrokes that result in something not listed.
source share