c# combobox set selected item by item's text
PROGRAMMING HELP 2017/10/05 02:52
C# Combobox defined with name my_combobox
Define string variable with the text of the combobox item that has to be selected. For example, if the combobox items have text values "item_a" "item_b" "item_c" ... and the item which text is "item_b" has to be selected
private System.Windows.Forms.ComboBox my_combobox;
Define string variable with the text of the combobox item that has to be selected. For example, if the combobox items have text values "item_a" "item_b" "item_c" ... and the item which text is "item_b" has to be selected
string select_this_item = "item_b"; string item_text = ""; int i = 0; for (i = 0; i < my_combobox.Items.Count; i++) { item_text = my_combobox.GetItemText(my_combobox.Items[i]); if (item_text == select_this_item) { my_combobox.SelectedItem = (object)my_combobox.Items[i]; break; } }