在C#中,我有变量, a
,类型的string
。
如何find item
的价值a
在combobox
(我想和值组合框没有显示文本找到项目)。
在C#中,我有变量, a
,类型的string
。
如何find item
的价值a
在combobox
(我想和值组合框没有显示文本找到项目)。
您可以通过使用下面的代码中找到它。
int index = comboBox1.Items.IndexOf(a);
为了获得该项目本身,写:
comboBox1.Items[index];
你应该看到的组合框控件的FindStringExact(),它会搜索将DisplayMember如果发现返回项目的索引的方法。 如果没有找到将返回-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
}
我知道我的解决方案很简单,有趣,但我训练之前,我用它。 重要提示:组合框的DropDownStyle必须是“DropDownList的”!
首先在组合框,然后:
bool foundit = false;
String mystr = "item_1";
mycombobox.Text = mystr;
if (mycombobox.SelectedText == mystr) // Or using mycombobox.Text
foundit = true;
else foundit = false;
这对我的作品的权利,并解决了我的问题......但是,从@ ST-mnmn的方式(解决方案)是更好的罚款。
嗨,大家好,如果最好的方法搜索文本或值
int Selected;
int count = ComboBox1.Items.Count;
for (int i = 0; (i<= (count - 1)); i++)
{
ComboBox1.SelectedIndex = i;
if ((string)(ComboBox1.SelectedValue) == "SearchValue")
{
Selected = i;
}
}
ComboBox1.SelectedIndex = Selected;