Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find an item by value in an combobox in C#?

In C#, I have variable, a, of type string.

How do I find item by value of a in combobox (I want find item with value no display text of combobox).

like image 668
Duy Khanh Avatar asked Apr 15 '12 08:04

Duy Khanh


2 Answers

You can find it by using the following code.

int index = comboBox1.Items.IndexOf(a);

To get the item itself, write:

comboBox1.Items[index];
like image 114
st mnmn Avatar answered Nov 18 '22 14:11

st mnmn


You should see a method on the combo box control for FindStringExact(), which will search the displaymember and return the index of that item if found. If not found will return -1.

//to select the item if found: 
mycombobox.SelectedIndex = mycombobox.FindStringExact("Combo"); 

//to test if the item exists: 
int i = mycombobox.FindStringExact("Combo"); 
if(i >= 0)
{
  //exists
}
like image 40
Andrew Mulvaine Avatar answered Nov 18 '22 12:11

Andrew Mulvaine