In WinForms 2.0, a ComboBox has an Auto-Complete feature, that displays a custom Drop-Down list with only the values that start with the entered text.
However, if I want to limit valid values to only those that appear in the ComboBox's list of items, I can do that by setting the DropDownStyle
to DropDownList
, which stops the user from entering a value.
However, now I can't use the Auto-Complete feature, which requires user input.
Is there another way to limit input to the list, while still allowing use of the Auto-Complete feature? Note that I have seen some custom solutions for this, but I really like the way the matching Auto-Complete items are displayed in a Drop-Down list, and sorted even though the original list may not be.
EDIT: I have thought about just validating the entered value, i.e. testing user input if it is valid in, say, the TextChanged
event, or even using the Validating
event. The question then is what is the expected behavior? Do I clear their value (an empty value is also invalid), or do I use a default value? Closest matching value?
P.s. Is there any other tags that I could add to this question?
The AutoComplete properties like AutoCompleteCustomSource, AutoCompleteMode and AutoCompleteSource to perform a TextBox that automatically completes user entry strings by comparing the initial letters being entered to the prefixes of all strings in a data source.
We can select any option in the dropdown list. Now add state = "readonly" in the Combobox object, it will make the Combobox Entry Widget disabled.
By default, DropDownStyle property of a Combobox is DropDown. In this case user can enter values to combobox. When you change the DropDownStyle property to DropDownList, the Combobox will become read only and user can not enter values to combobox.
This solution worked for me:
Private Sub myComboBox_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles myComboBox.Validating
If Not myComboBox.Items.Contains(myComboBox.Text) Then
MsgBox("Please select a value from the list", MsgBoxStyle.Exclamation, "Value not available")
e.Cancel = True
End If
End Sub
Have you tried setting AutoCompleteMode = AutoCompleteMode.SuggestAppend
and AutoCompleteSource = AutoCompleteSource.ListItems
? That lets the user type, but it only accepts words that are in the ComboBox
. The only catch is that the behavior has changed for Win7 (see ComboBox.SelectedValue does not match displayed text when DropDownStyle = DropDownList in Windows 7).
As for tags, you might try "combobox" and ".net".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With