我有一个字符串“test1”,我的组合框包含test1、test2和test3。如何设置选中项为“test1”?也就是说,我如何匹配我的字符串到一个组合框项目?
我想的是下面这行,但这不行。
comboBox1.SelectedText = "test1";
我有一个字符串“test1”,我的组合框包含test1、test2和test3。如何设置选中项为“test1”?也就是说,我如何匹配我的字符串到一个组合框项目?
我想的是下面这行,但这不行。
comboBox1.SelectedText = "test1";
当前回答
SelectedText用于在字符串编辑器中获取或设置组合框中所选项目的实际文本。如果你设置了:
comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
Use:
comboBox1.SelectedItem = "test1";
or:
comboBox1.SelectedIndex = comboBox1.Items.IndexOf("test1");
其他回答
ComboBox1.SelectedIndex= ComboBox1.FindString("Matching String");
在windows窗体中尝试此操作。
我已经创建了一个函数,它将返回值的索引
public static int SelectByValue(ComboBox comboBox, string value)
{
int i = 0;
for (i = 0; i <= comboBox.Items.Count - 1; i++)
{
DataRowView cb;
cb = (DataRowView)comboBox.Items[i];
if (cb.Row.ItemArray[0].ToString() == value)// Change the 0 index if your want to Select by Text as 1 Index
{
return i;
}
}
return -1;
}
_cmbTemplates.SelectedText = "test1"
或者
_cmbTemplates.SelectedItem= _cmbTemplates.Items.Equals("test1");
comboBox1.SelectedItem.Text = "test1";
应该可以
Yourcomboboxname.setselecteditem("yourstring");
如果你想设置数据库字符串,使用这个
Comboboxname.setselecteditem(ps.get string("databasestring"));