我有一个字符串“test1”,我的组合框包含test1、test2和test3。如何设置选中项为“test1”?也就是说,我如何匹配我的字符串到一个组合框项目?

我想的是下面这行,但这不行。

comboBox1.SelectedText = "test1"; 

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

comboBox1.SelectedItem = "test1";

在组合框中枚举ListItems 获得相等的列表索引集组合框 将listindex设置为找到的值。

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


在组合框中没有这个属性。你有SelectedItem或SelectedIndex。如果你有用来填充组合框的对象,那么你可以使用SelectedItem。

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

希望能有所帮助。


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

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

请记住,如果没有找到项目,IndexOf函数可能会抛出一个参数异常。


假设test1、test2、test3属于comboBox1集合,下面的语句将工作。

comboBox1.SelectedIndex = 0; 

_cmbTemplates.SelectedText = "test1"

或者

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

这应该可以达到目的:

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

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

comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

Use:

comboBox1.SelectedItem = "test1";

or:

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

你试过文本属性了吗?这对我很管用。

ComboBox1.Text = "test1";

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


对我来说,这只管用:

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

请试试这个方法,它对我很有效:

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

comboBox1.SelectedItem.Text = "test1";

我已经填充了我的组合框与从数据库填充的数据表。然后我设置了DisplayMember和ValueMember。我使用这段代码来设置所选项目。

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

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

It finds exact or PART of string and sets it. 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; }

希望我能帮上忙!


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

你的案子可以用

DropDownList.Items.FindByText("Text");

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

我使用了一个扩展方法:

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

我使用KeyValuePair ComboBox数据绑定,我想通过值找到项目,所以这在我的情况下工作:

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

应该可以

Yourcomboboxname.setselecteditem("yourstring");

如果你想设置数据库字符串,使用这个

Comboboxname.setselecteditem(ps.get string("databasestring"));

所有设置组合框项的方法、技巧和代码行都将在组合框具有父级之前不起作用。


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

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

你可以说comboBox1。[0].ToString();


这对我有用.....

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

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

在windows窗体中尝试此操作。


在组合框中找到mySecondObject (MyObject类型)(包含MyObjects列表)并选择该项:

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

我知道这不是执勤人员要求的,但会不会是他们不知道呢?这里已经有几个答案,所以即使这很长,我认为它可能对社区有用。

使用枚举填充组合框可以方便地使用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();
    }

如果你通过数据集绑定数据源,那么你应该使用"SelectedValue"

cmbCategoryList.SelectedValue = (int)dsLookUp.Tables[0].Select("WHERE PRODUCTCATEGORYID = 1")[0]["ID"];