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

时间:2009-01-16 10:50:23

标签: c# winforms combobox

我有一个字符串“test1”,我的comboBox包含test1test2test3。如何将所选项目设置为“test1”?也就是说,如何将我的字符串与其中一个comboBox项匹配?

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

comboBox1.SelectedText = "test1"; 

25 个答案:

答案 0 :(得分:252)

这应该可以解决问题:

Combox1.SelectedIndex = Combox1.FindStringExact("test1")

答案 1 :(得分:201)

您是否尝试过Text财产?这个对我有用。

ComboBox1.Text = "test1";

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

答案 2 :(得分:47)

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

comboBox1.SelectedIndex = comboBox1.Items.IndexOf("test1");

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

答案 3 :(得分:35)

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

comboBox1.SelectedItem = "test1";

答案 4 :(得分:10)

对我而言,这只有效:

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;
    }
}

答案 5 :(得分:7)

SelectedText用于获取或设置组合框中所选项目的字符串编辑器中的实际文本,如文档here所示。如果你设置:

,这是不可编辑的
comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

使用:

comboBox1.SelectedItem = "test1";

或:

comboBox1.SelectedIndex = comboBox1.Items.IndexOf("test1");

答案 6 :(得分: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;
        }
    } 
}

然后只使用方法:

ddl.SelectItemByValue(value);

答案 7 :(得分:6)

ComboBox1.SelectedIndex= ComboBox1.FindString("Matching String");

在Windows Form中尝试。

答案 8 :(得分:5)

comboBox1.SelectedItem.Text = "test1";

答案 9 :(得分:4)

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

comboBox1.SelectedIndex = 0; 

答案 10 :(得分:4)

此解决方案基于MSDN并进行了一些修改。

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

    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;
    }
    

我希望我帮忙!

答案 11 :(得分:3)

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

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

答案 12 :(得分:1)

  • 在combobox中枚举ListItem
  • 获得相同的listindex set combobox
  • 将listindex设置为找到的。

但如果我看到代码审查员这样的代码,我建议重新考虑所有方法算法。

答案 13 :(得分:1)

我使用 KeyValuePair 进行ComboBox数据绑定,我想通过查找项目,所以这适用于我的情况:

comboBox.SelectedItem = comboBox.Items.Cast<KeyValuePair<string,string>>().First(item=> item.Value == "value to match");

答案 14 :(得分:1)

_cmbTemplates.SelectedText = "test1"

或者

_cmbTemplates.SelectedItem= _cmbTemplates.Items.Equals("test1");

答案 15 :(得分:1)

ComboBox中没有该属性。您有SelectedItem或SelectedIndex。如果您有用于填充组合框的对象,则可以使用SelectedItem。

如果不是,您可以获取项目集合(属性项目)并迭代它,直到获得所需的值并将其与其他属性一起使用。

希望它有所帮助。

答案 16 :(得分:0)

  ListItem li = DropDownList.Items.FindByValue("13001");
  DropDownList.SelectedIndex = ddlCostCenter.Items.IndexOf(li);

对于您的情况,您可以使用

DropDownList.Items.FindByText("Text");

答案 17 :(得分:0)

combo.Items.FindByValue("1").Selected = true;

答案 18 :(得分:0)

在ComboBox拥有父级之前,所有方法,技巧和代码行设置ComboBox项都不起作用。

答案 19 :(得分:0)

我创建了一个函数,它将返回值的索引

        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;
        }

答案 20 :(得分:0)

这对我有用.....

comboBox.DataSource.To<DataTable>().Select(" valueMember = '" + valueToBeSelected + "'")[0]["DislplayMember"];

答案 21 :(得分:0)

在组合框(包含MyObjects列表)中找到mySecondObject(属于MyObject类型),然后选择以下项目:

foreach (MyObject item in comboBox.Items)
{
   if (item.NameOrID == mySecondObject.NameOrID)
    {
        comboBox.SelectedItem = item;
        break;
    }
}

答案 22 :(得分:0)

我知道这不是 OP 所问的,但他们可能不知道吗?这里已经有几个答案了,所以即使这很长,我认为它可能对社区有用。

使用枚举填充组合框允许轻松使用 SelectedItem 方法以编程方式选择组合框中的项目以及从组合框中加载和读取。

public enum Tests
    {
        Test1,
        Test2,
        Test3,
        None
    }

// Fill up combobox with all the items in the Tests enum
    foreach (var test in Enum.GetNames(typeof(Tests)))
    {
        cmbTests.Items.Add(test);
    }

    // Select combobox item programmatically
    cmbTests.SelectedItem = Tests.None.ToString();

如果双击组合框,您可以处理选定的索引更改事件:

private void cmbTests_SelectedIndexChanged(object sender, EventArgs e)
{
    if (!Enum.TryParse(cmbTests.Text, out Tests theTest))
    {
        MessageBox.Show($"Unable to convert {cmbTests.Text} to a valid member of the Tests enum");
        return;
    }

    switch (theTest)
    {
        case Tests.Test1:
            MessageBox.Show("Running Test 1");
            break;

        case Tests.Test2:
            MessageBox.Show("Running Test 2");
            break;

        case Tests.Test3:
            MessageBox.Show("Running Test 3");
            break;

        case Tests.None:

            // Do nothing

            break;

        default:
            MessageBox.Show($"No support for test {theTest}.  Please add");
            return;
    }
}

然后您可以从按钮单击处理程序事件运行测试:

 private void btnRunTest1_Click(object sender, EventArgs e)
    {
        cmbTests.SelectedItem = Tests.Test1.ToString();
    }

答案 23 :(得分:-1)

您可以说comboBox1.Text = comboBox1.Items[0].ToString();

答案 24 :(得分:-2)

请尝试这种方式,它对我有用:

Combobox1.items[Combobox1.selectedIndex] = "replaced text";