如何在C#中的组合框中按值查找项目?

Duy*_*anh 17 c# combobox items find

在C#中,我有a类型的变量string.

我如何find item通过ain 的值combobox(我想找到值没有组合框的显示文本的项目).

st *_*nmn 28

您可以使用以下代码找到它.

int index = comboBox1.Items.IndexOf(a);
Run Code Online (Sandbox Code Playgroud)

要获取项目本身,请写:

comboBox1.Items[index];
Run Code Online (Sandbox Code Playgroud)


小智 10

您应该在FindStringExact()的组合框控件上看到一个方法,该方法将搜索显示成员并返回该项目的索引(如果找到).如果未找到则返回-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
}
Run Code Online (Sandbox Code Playgroud)

  • 我认为FindExactString()在ComboBox的DisplayMember属性上起作用。我认为问题是关于如何在ComboBox的ValueMember属性上进行匹配。 (2认同)