Combobox and autocomplete in C#

20,300

Solution 1

I had the same problem and solved it this way:

private void comboBox_DropDown(object sender, EventArgs e)
{
    ComboBox cbo = (ComboBox)sender;
    cbo.PreviewKeyDown += new PreviewKeyDownEventHandler(comboBox_PreviewKeyDown);
}

private void comboBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    ComboBox cbo = (ComboBox)sender;
    cbo.PreviewKeyDown -= comboBox_PreviewKeyDown;
    if (cbo.DroppedDown) cbo.Focus();
}

Once the user clicks on the DropDown button PreviewKeyDown event is attached to that ComboBox. When user starts typing, freshly added event is triggered. In that event we check if ComboBox is DroppedDown, if it is, focus that ComboBox. On ComboBox focus DropDown disappeares and that's it.

Solution 2

What about using the DropDown and DropDownClosed events to disable or change the auto-complete mode?

Solution 3

I was having exactly the same problem. I tried the DropDown and DropDownClosed events to set the AutoCompleteMode property to none and suggest. In this situation the SelectedIndexChanged event does not get fired after selecting an item with the mouse. I was using the SelectedValue property in the SelectedIndexChanged event and this property is already changed at the moment the DropDownClosed event is triggered. In my case I simply called the SelectedIndexChanged method from the DropDownClosed event to solve the problem.

Solution 4

Implement event on ComboBox KeyDown. It should look like this.

void cmbExample_KeyDown(object sender, KeyEventArgs e)
    {
        if ((sender as ComboBox).DroppedDown)
            (sender as ComboBox).DroppedDown = false;
    }

Solution 5

Have you tried the other possible values for AutoCompleteMode, which are Append, None, and Suggest? I think that what you are looking for is Suggest instead of AppendSuggest.

Here is some downloadable sample code illustrating the different modes, if you need it.

Share:
20,300
Elfoc
Author by

Elfoc

Updated on July 09, 2022

Comments

  • Elfoc
    Elfoc almost 2 years

    i have small problem with autocomplete option in combobox. Everything is working correct, except that i want to work it diffrent :)

    When I start typing in combobox, autusuggest working the way i like :

    Combo

    But when i first open combobox, and then start typing i get something like that:

    enter image description here

    What's more i can't pick item from autosuggest combobox, only from this list under.

    AutocompleteMode is SuggestAppend

    I'd like to have autosuggest like on the first picture, and in situations like picture 2, this first combobox list should be closed somehow..

  • Elfoc
    Elfoc about 13 years
    I've tried them. Append is to show rest of the value, and suggest is this new combobox window. I'd like to have this new combobox with suggest mode. But then this first list should be closed..
  • bsegraves
    bsegraves about 12 years
    Yep, that worked for me. Here's some code that you can use: m_cmbNode.DropDown += (sender, e) => m_cmbNode.AutoCompleteMode = AutoCompleteMode.None; m_cmbNode.DropDownClosed += (sender, e) => m_cmbNode.AutoCompleteMode = AutoCompleteMode.SuggestAppend;