我有一个字符串“test1”,我的组合框包含test1、test2和test3。如何设置选中项为“test1”?也就是说,我如何匹配我的字符串到一个组合框项目?
我想的是下面这行,但这不行。
comboBox1.SelectedText = "test1";
我有一个字符串“test1”,我的组合框包含test1、test2和test3。如何设置选中项为“test1”?也就是说,我如何匹配我的字符串到一个组合框项目?
我想的是下面这行,但这不行。
comboBox1.SelectedText = "test1";
当前回答
你试过文本属性了吗?这对我很管用。
ComboBox1.Text = "test1";
SelectedText属性用于组合框的文本框部分中可编辑文本的选定部分。
其他回答
combo.Items.FindByValue("1").Selected = true;
如果组合框中的项目是字符串,您可以尝试:
comboBox1.SelectedItem = "test1";
我使用KeyValuePair ComboBox数据绑定,我想通过值找到项目,所以这在我的情况下工作:
comboBox.SelectedItem = comboBox.Items.Cast<KeyValuePair<string,string>>().First(item=> item.Value == "value to match");
_cmbTemplates.SelectedText = "test1"
或者
_cmbTemplates.SelectedItem= _cmbTemplates.Items.Equals("test1");
在组合框中找到mySecondObject (MyObject类型)(包含MyObjects列表)并选择该项:
foreach (MyObject item in comboBox.Items)
{
if (item.NameOrID == mySecondObject.NameOrID)
{
comboBox.SelectedItem = item;
break;
}
}