如何使用C#设置组合框中的选定项以匹配字符串?

如何使用C#设置组合框中的选定项以匹配字符串?,c#,winforms,combobox,C#,Winforms,Combobox,我有一个字符串“test1”,我的组合框包含test1、test2和test3。如何将所选项目设置为“test1”?也就是说,如何将我的字符串与组合框项目之一匹配 我在想下面这行,但这行不通 comboBox1.SelectedText = "test1"; 如果组合框中的项目是字符串,则可以尝试: comboBox1.SelectedItem = "test1"; 枚举组合框中的列表项 获取相等的列表索引集组合框 将listindex设置为找到的索引 但是如果我以代码审阅者的身份看到

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

我在想下面这行,但这行不通

comboBox1.SelectedText = "test1"; 

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

comboBox1.SelectedItem = "test1";
  • 枚举组合框中的列表项
  • 获取相等的列表索引集组合框
  • 将listindex设置为找到的索引

但是如果我以代码审阅者的身份看到这样的代码,我建议重新考虑所有的方法算法。

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

如果没有,您可以获取项集合(属性项)并进行迭代,直到获得所需的值并将其与其他属性一起使用


希望有帮助。

假设您的组合框没有数据绑定,您需要在表单的“items”集合中找到对象的索引,然后将“selectedindex”属性设置为相应的索引

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

请记住,如果找不到项,IndexOf函数可能会抛出argumentexception。

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

comboBox1.SelectedIndex = 0; 
或许

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

这应该可以做到:

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

SelectedText是在字符串编辑器中获取或设置组合框中所选项目的实际文本,如文档所示。如果您设置以下内容,则此项不可编辑:

comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
使用:

或:

你试过酒店吗?它对我有用

ComboBox1.Text = "test1";

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

对于我来说,这仅适用于:

foreach (ComboBoxItem cbi in someComboBox.Items)
{
    if (cbi.Content as String == "sometextIntheComboBox")
    {
        someComboBox.SelectedItem = cbi;
        break;
    }
}
MOD:如果您在combobox中设置了自己的对象作为项目,则用其中一个项目替换ComboBoxItem,如下所示:

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

请尝试这种方式,它适合我:

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

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

foreach (DataRowView Row in ComboBox1.Items)
{
    if (Row["ColumnName"].ToString() == "Value") ComboBox1.SelectedItem = Row;
}
这个解决方案基于我所做的一些修改

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

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

我使用了一种扩展方法:

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

我对组合框数据绑定使用了KeyValuePair,我想通过value查找项,因此在我的情况下这是有效的:

comboBox.SelectedItem = comboBox.Items.Cast<KeyValuePair<string,string>>().First(item=> item.Value == "value to match");
comboBox.SelectedItem=comboBox.Items.Cast().First(item=>item.Value==“要匹配的值”);
它应该能工作

Yourcomboboxname.setselecteditem("yourstring");
如果要设置数据库字符串,请使用

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

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

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

        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;
        }
public static int SelectByValue(组合框、组合框、字符串值)
{
int i=0;

对于(i=0;i你可以说
comboBox1.Text=comboBox1.Items[0].ToString();

这对我很有用

comboBox.DataSource.To<DataTable>().Select(" valueMember = '" + valueToBeSelected + "'")[0]["DislplayMember"];
comboBox.DataSource.To().Select(“valueMember=”+valueToBeSelected+”)[0][“dislplayember”];
在windows窗体中尝试此操作。

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

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

用这段代码,你给SelectedItem属性分配了一个bool…这对我来说是行不通的。这肯定只是在组合框的可编辑区域设置文本,而不是从列表中选择相关项?如果列表项集合包含对象而不仅仅是字符串,那么我怀疑这会选择合适的ListItem对象,而不是它只会设置ComboBox上的Text属性?它会设置控件的SelectedValue属性以防万一:只有在填充ComboBox后才能设置此参数。当ComboBox.DropDownStyle=ComboBoxStyle.DropDownList;My ComboBox DropDownStyle为DropDownList和.Text=“某些文本”时,它不起作用不起作用。此解决方案对我很有效:Combox1.SelectedIndex=Combox1.FindStringExact(“test1”)不,不是:已保存日期!所选项目必须与类型匹配-对我来说就是这样!我一直在挠头,为什么即使项目明显存在,也没有设置所选项目-结果是类型不匹配!为了完整性,从上面的链接获得描述非常有用,特别是因为此答案执行匹配检查建议被其他答案(包括@norbertB的首选答案)覆盖:当您将SelectedItem属性设置为对象时,组合框将尝试使该对象成为列表中当前选定的对象。如果在列表中找到该对象,则该对象将显示在组合框的编辑部分,SelectedIndex属性将设置为相应的index。如果列表中不存在该对象,则SelectedIndex属性将保留其当前值。comboBox1.Items.IndexOf…如果Items为空,则可能出现NullReferenceException。@Gary:不,它不存在,
Items
将始终是非null集合,即使集合中没有任何项(
Items.Count==0
)如果为“1”,则也可能出现例外情况找不到。您应该首先检查selectedIndex是否为-1,或者更准确地说,它是否为>=0和<.items.length。这对我来说很有效,但您需要注意的是,ComboBox中的项目实际上是ComboBoxItems,因为可以将其他项目也放在其中。这在WinRT 8.1中非常有效。实际上,我用像dave在他的SelectItemByValue()解决方案中所写的扩展方法,这确实是一个完美的解决方案。对我来说,这似乎是一个更好的答案,因为您有一个返回值,可以临时保存在变量中,并用它来测试您是否找到了要查找的值。如果有呢
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;
        }
comboBox.DataSource.To<DataTable>().Select(" valueMember = '" + valueToBeSelected + "'")[0]["DislplayMember"];
ComboBox1.SelectedIndex= ComboBox1.FindString("Matching String");
foreach (MyObject item in comboBox.Items)
{
   if (item.NameOrID == mySecondObject.NameOrID)
    {
        comboBox.SelectedItem = item;
        break;
    }
}