Check specific value in combo box

How can I verify that the combobox in winforms contains some kind of value?

Is there a way to do this without repeating all the items?

+9
source share
5 answers
if (comboBox1.Items.Contains("some value")) { } 

If the elements are a custom object instead of strings, you may need to override the Equals method.

+16
source

int index = comboBox1.FindString("some value"); comboBox1.SelectedIndex = index;

http://msdn.microsoft.com/en-us/library/wxyt1t12.aspx#Y500

There is also FindStringExact http://msdn.microsoft.com/en-us/library/c440x2eb.aspx

+7
source

Other answers did not help me.

It:

 if (comboBox1.Items.Cast<string>().Any(i => i == position)) { // Items contains value } 

Hope this helps!

+2
source

To find the exact data from the combo box, we have to check with FindStringExact

int resultIndex = cbEmployee1.FindStringExact (item.Text);

+2
source

Using the accepted answer did not work for me, as it always returned false, even if checking the list shows the existing value. I use the FindStringExact method, as recommended by Louis and Amit. In this case, this value is entered in the comboBox text box.

 var index = comboBox1.FindStringExact(comboBox1.Text) if(index > -1) { //Success followup code } 
+1
source

All Articles