Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the AutoComplete feature of ComboBox, while limiting values to those in the list?

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?

like image 322
Schmuli Avatar asked Jan 14 '09 16:01

Schmuli


People also ask

What is AutoComplete Combobox?

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.

How do I disable writing in Combobox?

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.

Is the default event of Combobox control?

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.


2 Answers

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
like image 133
StevenC Avatar answered Sep 21 '22 06:09

StevenC


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".

like image 24
Ecyrb Avatar answered Sep 22 '22 06:09

Ecyrb