我有一个字符串“test1”,我的组合框包含test1、test2和test3。如何设置选中项为“test1”?也就是说,我如何匹配我的字符串到一个组合框项目?
我想的是下面这行,但这不行。
comboBox1.SelectedText = "test1";
我有一个字符串“test1”,我的组合框包含test1、test2和test3。如何设置选中项为“test1”?也就是说,我如何匹配我的字符串到一个组合框项目?
我想的是下面这行,但这不行。
comboBox1.SelectedText = "test1";
当前回答
ComboBox1.SelectedIndex= ComboBox1.FindString("Matching String");
在windows窗体中尝试此操作。
其他回答
ListItem li = DropDownList.Items.FindByValue("13001");
DropDownList.SelectedIndex = ddlCostCenter.Items.IndexOf(li);
你的案子可以用
DropDownList.Items.FindByText("Text");
请试试这个方法,它对我很有效:
Combobox1.items[Combobox1.selectedIndex] = "replaced text";
这应该可以达到目的:
Combox1.SelectedIndex = Combox1.FindStringExact("test1")
如果组合框中的项目是字符串,您可以尝试:
comboBox1.SelectedItem = "test1";
在组合框中没有这个属性。你有SelectedItem或SelectedIndex。如果你有用来填充组合框的对象,那么你可以使用SelectedItem。
如果不是,您可以获得项的集合(属性items),并迭代它,直到您获得想要的值,并将其与其他属性一起使用。
希望能有所帮助。