Only select list items from combobox

I have a winforms application in visual studio 2010.

On a form, I have a databound combobox, for which i have set autocompletesource=listitems and autocompletemode=suggestappend.

Now For this functionality to work, I have set dropdownstyle=dropdown, so that user can type a text

But I want a user to be able to select only an item available from its dropdown.

If user enters item other than list items, and leaves combobox, user should not be able to leave combobox.

In short, I want user to be able to select item only from available listitems, not anything he enters.

plz help


If you set DropDownStyle = DropDownList and AutoCompleteMode = Append , the user will still be able to type the value to select the item they want, but they will be limited to the items that are in the list.

When AutoCompleteMode = Append , it will check subsequent characters typed by appending them to the value being searched, as long as you type them quickly, that is. If you wait too long between key strokes, then it will go back to the first letter search again.

Consider: do you really need them to be able to enter an invalid value just so you can alert them that it's invalid? Because if not, it's just more confusing that way. By giving them the opportunity to enter any value, it implies that they are allowed to do so.


Set the property 'DropDownStyle' to 'DropdownList' and this will stop the user from typing into the combo.

Hope this helps.


I look for some solution but without using limiting DropDownList (typing is time limited users must be quick).

Previous code seems good for me, but is not called during typing what we required. ComboBox I switch to AutoCompleteMode = SuggestAppend , AutoCompleteSource = ListItems , DoprDownStyle = DropDown . This allow user directly typing to box and is not time limited.

This is my code I hope will help to some one:

Private Sub ComboBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyUp
  If ComboBox1.Text <> String.Empty Then
    If ComboBox1.FindString(cboSkupina.Text) = -1 Then 'if is value -1 typed text is not in list
      ComboBox1.Text = Mid(ComboBox1.Text, 1, Len(ComboBox1.Text) - 1) 'Delete not valid character
      ComboBox1.SelectionStart = Len(ComboBox1.Text) + 1 'Place cursor at the end
    End If
  End If
End Sub
链接地址: http://www.djcxy.com/p/60904.html

上一篇: 允许输入值或自动完成

下一篇: 仅从组合框中选择列表项