Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the selected item in a comboBox to match my string using C#?

People also ask

How do I set ComboBox options?

To add items to a ComboBox, select the ComboBox control and go to the properties window for the properties of this control. Click the ellipses (...) button next to the Items property. This opens the String Collection Editor dialog box, where you can enter the values one at a line.

How do I index my ComboBox?

The Combobox widget allows you to create a dropdown list in which the list of items can be selected instantly. However, if you want to get the index of selected items in the combobox widget, then you can use the get() method. The get() method returns an integer of the selected item known as the index of the item.


This should do the trick:

Combox1.SelectedIndex = Combox1.FindStringExact("test1")

Have you tried the Text property? It works for me.

ComboBox1.Text = "test1";

The SelectedText property is for the selected portion of the editable text in the textbox part of the combo box.


Assuming that your combobox isn't databound you would need to find the object's index in the "items" collection on your form and then set the "selectedindex" property to the appropriate index.

comboBox1.SelectedIndex = comboBox1.Items.IndexOf("test1");

Keep in mind that the IndexOf function may throw an argumentexception if the item isn't found.


If the items in your ComboBox are strings, you can try:

comboBox1.SelectedItem = "test1";