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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/450059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:49:51  来源:igfitidea点击:

How do I set the selected item in a comboBox to match my string using C#?

c#winformscombobox

提问by

I have a string "test1" and my comboBox contains test1, test2, and test3. How do I set the selected item to "test1"? That is, how do I match my string to one of the comboBox items?

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

I was thinking of the line below, but this doesn't work.

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

comboBox1.SelectedText = "test1"; 

回答by Frederik Gheysels

If the items in your ComboBox are strings, you can try:

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

comboBox1.SelectedItem = "test1";

回答by user53378

  • Enumerate ListItems in combobox
  • Get equal ones listindex set combobox
  • Set listindex to the found one.
  • 枚举组合框中的列表项
  • 获取相等的列表索引集组合框
  • 将 listindex 设置为找到的一个。

But if I see such a code as a code reviewer, I would recommend to reconsider all the method algorithm.

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

回答by Megacan

You don't have that property in the ComboBox. You have SelectedItem or SelectedIndex. If you have the objects you used to fill the combo box then you can use SelectedItem.

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

If not you can get the collection of items (property Items) and iterate that until you get the value you want and use that with the other properties.

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

hope it helps.

希望能帮助到你。

回答by Spence

Assuming that your combobox isn't databound you would need to find the object's index in the "items" collection on your form and then set the "selectedindex" property to the appropriate index.

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

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

Keep in mind that the IndexOf function may throw an argumentexception if the item isn't found.

请记住,如果未找到该项目,IndexOf 函数可能会抛出参数异常。

回答by ihcarp

Supposing test1, test2, test3 belong to comboBox1 collection following statement will work.

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

comboBox1.SelectedIndex = 0; 

回答by Dean

_cmbTemplates.SelectedText = "test1"

or maybe

或者可能

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

回答by Norbert B.

This should do the trick:

这应该可以解决问题:

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

回答by Brian Rudolph

SelectedText is to get or set the actual text in the string editor for the selected item in the combobox as documented here. This goes uneditable if you set:

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

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

Use:

用:

comboBox1.SelectedItem = "test1";

or:

或者:

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

回答by Andrew Kennan

Have you tried the Textproperty? It works for me.

您是否尝试过Text属性?这个对我有用。

ComboBox1.Text = "test1";

The SelectedText property is for the selected portion of the editable text in the textbox part of the combo box.

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

回答by gabore

For me this worked only:

对我来说,这只有效:

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

MOD: and if You have your own objects as items set up in the combobox, then substitute the ComboBoxItem with one of them like:

MOD:如果您有自己的对象作为组合框中设置的项目,则将 ComboBoxItem 替换为其中之一,例如:

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