C#ではa
、タイプが。の変数がありますstring
。
infind 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 = -1;
int count = ComboBox1.Items.Count;
for (int i = 0; (i<= (count - 1)); i++)
{
ComboBox1.SelectedIndex = i;
if ((string)(ComboBox1.SelectedValue) == "SearchValue")
{
Selected = i;
break;
}
}
ComboBox1.SelectedIndex = Selected;