C # Combobox (Dropdownstyle = Simple) - how to select an element as you type

I have a Combobox control in my form (WinForms, .NET 3.5), and the DropDownStyle property is set to Simple . Let them say that it is filled with letters of the alphabet, like a string of objects ("a", "b", "c", etc.).
When I enter a letter in the combobox input box, the correct item will appear just below.

This is the behavior that I want. But I would also like the first selected item selected.

Is there a property of the Combobox control that would achieve this? Or do I need to handle this programmatically?

+4
source share
1 answer

Depending on your needs, you might consider using a TextBox control and setting autocomplete properties (for example, AutoCompleteMode and AutoCompleteCustomSource).

The difficulty you will encounter is that after selecting an item (programmatically) the text in the combo box will change. So, let's do something like this:

private void comboBox1_TextChanged(object sender, EventArgs e) { for(int i=0; i<comboBox1.Items.Count; i++) { if (comboBox1.Items[i].ToString().StartsWith(comboBox1.Text)) { comboBox1.SelectedIndex = i; return; } } } 

can accomplish what you want (in terms of choice), but it will also immediately change the user's text.

+2
source

All Articles