我有一个字符串“test1”,我的组合框包含test1、test2和test3。如何设置选中项为“test1”?也就是说,我如何匹配我的字符串到一个组合框项目?
我想的是下面这行,但这不行。
comboBox1.SelectedText = "test1";
我有一个字符串“test1”,我的组合框包含test1、test2和test3。如何设置选中项为“test1”?也就是说,我如何匹配我的字符串到一个组合框项目?
我想的是下面这行,但这不行。
comboBox1.SelectedText = "test1";
当前回答
如果组合框中的项目是字符串,您可以尝试:
comboBox1.SelectedItem = "test1";
其他回答
对我来说,这只管用:
foreach (ComboBoxItem cbi in someComboBox.Items)
{
if (cbi.Content as String == "sometextIntheComboBox")
{
someComboBox.SelectedItem = cbi;
break;
}
}
MOD:如果你有你自己的对象作为项目设置在组合框,然后替换ComboBoxItem与他们中的一个:
foreach (Debitor d in debitorCombo.Items)
{
if (d.Name == "Chuck Norris")
{
debitorCombo.SelectedItem = d;
break;
}
}
_cmbTemplates.SelectedText = "test1"
或者
_cmbTemplates.SelectedItem= _cmbTemplates.Items.Equals("test1");
请试试这个方法,它对我很有效:
Combobox1.items[Combobox1.selectedIndex] = "replaced text";
所有设置组合框项的方法、技巧和代码行都将在组合框具有父级之前不起作用。
如果你通过数据集绑定数据源,那么你应该使用"SelectedValue"
cmbCategoryList.SelectedValue = (int)dsLookUp.Tables[0].Select("WHERE PRODUCTCATEGORYID = 1")[0]["ID"];