如何在comboBox中设置所选项以使用C#匹配我的字符串?

188 c# combobox winforms

我有一个字符串"测试1"和我的组合框包含test1,test2test3.如何将所选项目设置为"test1"?也就是说,如何将我的字符串与其中一个comboBox项匹配?

我在考虑下面这一行,但这不起作用.

comboBox1.SelectedText = "test1"; 
Run Code Online (Sandbox Code Playgroud)

nor*_*rtB 262

这应该做的伎俩:

Combox1.SelectedIndex = Combox1.FindStringExact("test1")
Run Code Online (Sandbox Code Playgroud)


And*_*nan 203

你试过Text属性吗?这个对我有用.

ComboBox1.Text = "test1";
Run Code Online (Sandbox Code Playgroud)

SelectedText属性用于组合框的文本框部分中可编辑文本的选定部分.

  • 我的组合框下拉样式是DropDownList,而.Text ="some text"不起作用.这个解决方案对我来说很好:Combox1.SelectedIndex = Combox1.FindStringExact("test1") (10认同)
  • 它确实设置了控件的SelectedValue属性 (9认同)
  • 当然,只需在ComboBox的可编辑区域中设置文本,而不是从列表中选择相关项目?如果列表项集合包含对象而不仅仅是字符串,那么我怀疑这会选择合适的ListItem对象,而只是在ComboBox上设置Text属性? (7认同)
  • 以防万一:只有在填充组合框后才能设置此参数. (3认同)

Spe*_*nce 48

假设你的组合框不是数据绑定的,你需要在表单的"items"集合中找到对象的索引,然后将"selectedindex"属性设置为适当的索引.

comboBox1.SelectedIndex = comboBox1.Items.IndexOf("test1");
Run Code Online (Sandbox Code Playgroud)

请记住,如果找不到该项,IndexOf函数可能会引发争论.


Fre*_*els 37

如果ComboBox中的项目是字符串,您可以尝试:

comboBox1.SelectedItem = "test1";
Run Code Online (Sandbox Code Playgroud)

  • 不,不是:http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem.aspx (18认同)

小智 10

对我来说这只有效:

foreach (ComboBoxItem cbi in someComboBox.Items)
{
    if (cbi.Content as String == "sometextIntheComboBox")
    {
        someComboBox.SelectedItem = cbi;
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

MOD:如果你有自己的对象作为组合框中设置的项目,那么用其中一个替换ComboBoxItem,如:

foreach (Debitor d in debitorCombo.Items)
{
    if (d.Name == "Chuck Norris")
    {
        debitorCombo.SelectedItem = d;
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这对我有用,但你需要注意ComboBox中的项目实际上是ComboBoxItems,因为它也可以放入其他项目. (2认同)

Bri*_*lph 8

SelectedText是获取或设置字符串编辑所选项目的实际文本在下拉列表作为记录在这里.如果你设置:这是不可编辑的:

comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
Run Code Online (Sandbox Code Playgroud)

使用:

comboBox1.SelectedItem = "test1";
Run Code Online (Sandbox Code Playgroud)

要么:

comboBox1.SelectedIndex = comboBox1.Items.IndexOf("test1");
Run Code Online (Sandbox Code Playgroud)


dav*_*ave 7

我使用了一种扩展方法:

public static void SelectItemByValue(this ComboBox cbo, string value)
{
    for(int i=0; i < cbo.Items.Count; i++)
    {
        var prop = cbo.Items[i].GetType().GetProperty(cbo.ValueMember);
        if (prop!=null && prop.GetValue(cbo.Items[i], null).ToString() == value)
        {
             cbo.SelectedIndex = i;
             break;
        }
    } 
}
Run Code Online (Sandbox Code Playgroud)

然后只需使用方法:

ddl.SelectItemByValue(value);
Run Code Online (Sandbox Code Playgroud)


Muh*_*ail 7

ComboBox1.SelectedIndex= ComboBox1.FindString("Matching String");
Run Code Online (Sandbox Code Playgroud)

在Windows窗体中尝试此操作。


小智 5

假设 test1、test2、test3 属于comboBox1集合,以下语句将起作用。

comboBox1.SelectedIndex = 0; 
Run Code Online (Sandbox Code Playgroud)


小智 5

comboBox1.SelectedItem.Text = "test1";
Run Code Online (Sandbox Code Playgroud)


小智 5

我已经用从数据库填充的 een DataTable 填充了 ComboBox。然后我设置了 DisplayMember 和 ValueMember。我使用此代码来设置所选项目。

foreach (DataRowView Row in ComboBox1.Items)
{
    if (Row["ColumnName"].ToString() == "Value") ComboBox1.SelectedItem = Row;
}
Run Code Online (Sandbox Code Playgroud)


小智 5

这个解决方案是基于MSDN我做了一些修改。

  • 它找到字符串的确切部分或部分并设置它。

    private int lastMatch = 0;
    private void textBoxSearch_TextChanged(object sender, EventArgs e)
    {
        // Set our intial index variable to -1.
        int x = 0;
        string match = textBoxSearch.Text;
        // If the search string is empty set to begining of textBox
        if (textBoxSearch.Text.Length != 0)
        {
            bool found = true;
            while (found)
            {
                if (comboBoxSelect.Items.Count == x)
                {
                    comboBoxSelect.SelectedIndex = lastMatch;
                    found = false;
                }
                else
                {
                    comboBoxSelect.SelectedIndex = x;
                    match = comboBoxSelect.SelectedValue.ToString();
                    if (match.Contains(textBoxSearch.Text))
                    {
                        lastMatch = x;
                        found = false;
                    }
                    x++;
                }
            }
        }
        else
            comboBoxSelect.SelectedIndex = 0;
    }
    
    Run Code Online (Sandbox Code Playgroud)

我希望我有所帮助!