Clyde
Clyde

Reputation: 8145

WinForms ComboBox DropDown and Autocomplete window both appear

I've got a ComboBox on a winforms app with this code:

comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

DataTable t = new DataTable();
t.Columns.Add("ID", typeof(int));
t.Columns.Add("Display", typeof(string));

for (int i = 1; i < 2000; i++)
{
    t.Rows.Add(i, i.ToString("N0"));
}

comboBox1.DataSource = t;
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "Display";

I then follow these steps when the window opens:

  1. Click the ComboBox drop down button -- this displays the list of items and selects the text in the ComboBox
  2. Type '5', '1' ... i.e. I'm looking to use autocomplete to search for 515, 516, etc.
  3. You'll see that the autocomplete window now appears ON TOP of the drop down list. However if I mouse over, it's the obscured drop down window behind the autocomplete window that's receiving the mouse events, including the click. So I think I'm clicking on an autocomplete item but actually clicking on something totally random that I can't see.

Is this a bug in the ComboBox? I'm using Windows 7 if that matters. Am I configuring the ComboBoxwrong somehow?

Note also that using the KEYBOARD uses the autocomplete drop down. So up/down arrow keys are using the front window, but the mouse is using the back window.

enter image description here

Upvotes: 34

Views: 53476

Answers (11)

Dr. Tom Way
Dr. Tom Way

Reputation: 1

Many years later, I encountered this same issue. While the actual answer (@JoeGayetty) works, it wasn't enough for me.

To preserve the desired suggestion behavior, I used an AutoCompleteMode of SuggestAppend and most importantly a DropDownStyle of DropDownList (which is not the default). Doing this corrected the duplicate drop-down problem for me while preserving the desired suggestion behavior:

myComboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend
myComboBox.DropDownStyle = ComboBoxStyle.DropDownList;

As a secondary data point, the original problem seems to only (I think) happen when the content of the combobox is wider than the control.

Upvotes: 0

Saurav Das
Saurav Das

Reputation: 11

Select the ComboBox from the design view and set "Append" to the AutoCompleteMode property, this will suggest the item without apearing a window.

Upvotes: 1

Saurav Das
Saurav Das

Reputation: 11

Select the ComboBox from the design view and set "None" to the AutoCompleteMode property.

Upvotes: 0

Joe Gayetty
Joe Gayetty

Reputation: 1561

Add a single line of code to your ComboBox KeyDown event and the problem is solved!

private void comboBox_NameAndID_KeyDown(object sender, KeyEventArgs e)
{
    comboBox_NameAndID.DroppedDown = false;
}

Source

Upvotes: 32

Lalit
Lalit

Reputation: 61

WinForms ComboBox DropDown...the answer is this...
write below code in comboBox1 Enter event..

private void comboBox1_Enter(object sender, EventArgs e)
{
    comboBox1.DroppedDown = true;
}

Now for comboBox1 AutoComplete...
write this AutoComplete() in page load event..so it work...

public void AutoComplete()
{
    try
    {
        MySqlConnection conn = new 
        MySqlConnection("server=localhost;database=databasename;user
            id=root;password=;charset=utf8;");
        MySqlCommand cmd = new MySqlCommand("select distinct
            (columnName) from tablename", conn);
        DataSet ds = new DataSet();
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        da.Fill(ds, "tablename");
        AutoCompleteStringCollection col = new
        AutoCompleteStringCollection();

        int i = 0;
        for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
            col.Add(ds.Tables[0].Rows[i]["columnName"].ToString());
        }
        comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
        comboBox1.AutoCompleteCustomSource = col;
        comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
    MessageBoxIcon.Error);
    }
}

Upvotes: 0

vivek verma
vivek verma

Reputation: 1

You simply add item in collection.

Now go properties option of combo box choose AutoCompleteSource=ListItems AutocompleteMode=suggest

note: autocomplete source have many option as per your requirement :)

Upvotes: 0

Wilton Alves
Wilton Alves

Reputation: 21

I'm a Brasilian student of encoding and I lose many hours seeking to fix it im my project. And here, I saw it in a few seconds!!!

My code seems like this:

private void populateCombos()
    {
        persist.ShowLst(dspMember, vlMember,varTable,lstBox,varWhere);
        persist.ShowLst(dspMember, vlMember,varTable,ddlist1,varWhere);
        persist.ShowLst(dspMember, vlMember,varTable, ddlist2,varWhere);

        ddList1.Text = null;
        ddList2.Text = null;

        lstBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        lstBox.AutoCompleteSource = AutoCompleteSource.ListItems;
        lstBox.Text = null;
    }

Upvotes: 2

ComboBoxNerd
ComboBoxNerd

Reputation: 11

Add to the/a keypress event.

    Dim box As ComboBox = sender
    box.DroppedDown = False

Upvotes: 1

AC25
AC25

Reputation: 423

to only have one open at a time you can use comboBox1.Droppeddown = true open up the regular, false the AutoComplete will only appear

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 942099

No problem getting a repro for this simply by setting the properties from the PropertyGrid. Behaves this way both in Win7 and Windows XP.

This is broken behavior documented in this feedback article. As indicated, Microsoft is not considering a fix. One possible workaround is to disable autocomplete in a DropDown event handler and re-enable it in a DropDownClosed event handler.

Upvotes: 20

J&#252;rgen Steinblock
J&#252;rgen Steinblock

Reputation: 31743

That's weired. Your code looks fine to me and I used this the AutoComplete feature a several times and it didn't show both the DropDown and the AutoComplete list.

My suggestion would be

  • Set the DataSource after the Display/Value Members. I can't remember why but the other caused some problems.

    comboBox1.ValueMember = "ID";
    comboBox1.DisplayMember = "Display";
    comboBox1.DataSource = t;
    
  • Set the AutoCompleteSource at the end of your code (after adding the DataSouce)

Maybe that helps.

Upvotes: 0

Related Questions