ComboBox:向项添加文本和值(无绑定源)

Boh*_*ohn 189 c# combobox winforms

在C#WinApp中,如何将Text和Value添加到我的ComboBox的项目中?我做了一个搜索,通常答案是使用"绑定到源"..但在我的情况下,我的程序中没有准备好绑定源...我怎么能这样做:

combo1.Item[1] = "DisplayText";
combo1.Item[1].Value = "useful Value"
Run Code Online (Sandbox Code Playgroud)

Ada*_*itz 343

您必须创建自己的类类型并覆盖ToString()方法以返回所需的文本.以下是您可以使用的类的简单示例:

public class ComboboxItem
{
    public string Text { get; set; }
    public object Value { get; set; }

    public override string ToString()
    {
        return Text;
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是其用法的简单示例:

private void Test()
{
    ComboboxItem item = new ComboboxItem();
    item.Text = "Item text1";
    item.Value = 12;

    comboBox1.Items.Add(item);

    comboBox1.SelectedIndex = 0;

    MessageBox.Show((comboBox1.SelectedItem as ComboboxItem).Value.ToString());
}
Run Code Online (Sandbox Code Playgroud)

  • 我相信这可能只在ASP.NET中可用,而不是在WinForms中. (14认同)
  • 我们如何获得"SelectedValue"或根据价值选择项目...请回复 (4认同)
  • 我们真的需要这个新类ComboboxItem吗?我认为已经有一个名为ListItem的存在. (3认同)

fab*_*fab 179

// Bind combobox to dictionary
Dictionary<string, string>test = new Dictionary<string, string>();
        test.Add("1", "dfdfdf");
        test.Add("2", "dfdfdf");
        test.Add("3", "dfdfdf");
        comboBox1.DataSource = new BindingSource(test, null);
        comboBox1.DisplayMember = "Value";
        comboBox1.ValueMember = "Key";

// Get combobox selection (in handler)
string value = ((KeyValuePair<string, string>)comboBox1.SelectedItem).Value;
Run Code Online (Sandbox Code Playgroud)

  • 工作完美,这应该是选定的答案.但是我们不能使用comboBox1.SelectedText而不是强制转换.SelectedItem并使用.Value吗? (2认同)

buh*_*tla 116

你可以像这样使用匿名类:

comboBox.DisplayMember = "Text";
comboBox.ValueMember = "Value";

comboBox.Items.Add(new { Text = "report A", Value = "reportA" });
comboBox.Items.Add(new { Text = "report B", Value = "reportB" });
comboBox.Items.Add(new { Text = "report C", Value = "reportC" });
comboBox.Items.Add(new { Text = "report D", Value = "reportD" });
comboBox.Items.Add(new { Text = "report E", Value = "reportE" });
Run Code Online (Sandbox Code Playgroud)

更新:虽然上面的代码将正确显示在组合框中,但您将无法使用SelectedValueSelectedText属性ComboBox.为了能够使用它们,绑定组合框如下:

comboBox.DisplayMember = "Text";
comboBox.ValueMember = "Value";

var items = new[] { 
    new { Text = "report A", Value = "reportA" }, 
    new { Text = "report B", Value = "reportB" }, 
    new { Text = "report C", Value = "reportC" },
    new { Text = "report D", Value = "reportD" },
    new { Text = "report E", Value = "reportE" }
};

comboBox.DataSource = items;
Run Code Online (Sandbox Code Playgroud)

  • 我想稍微修改一下,因为程序员可能需要一个for循环.而不是数组我使用列表`List <Object> items = new List <Object>();`然后我能够使用方法`items.Add(new {Text ="report A",Value ="reportA "});`在循环中. (13认同)
  • @Venkat如果使用设置`DataSource`的第二个解决方案,你可以使用组合框的`SelectedValue`或`SelectedText`属性,所以不需要做任何特殊的转换. (2认同)

小智 30

您应该使用dynamicobject在运行时解析组合框项目.

comboBox.DisplayMember = "Text";
comboBox.ValueMember = "Value";

comboBox.Items.Add(new { Text = "Text", Value = "Value" });

(comboBox.SelectedItem as dynamic).Value
Run Code Online (Sandbox Code Playgroud)

  • 这比创建一个单独的类并重写 ToString() 要好得多。 (2认同)
  • 那么,如何使用“值”设置正确的组合框项目呢? (2认同)
  • 这仍然是 2022 年(后期)最简单的...... (2认同)

小智 15

您可以使用DictionaryObject而不是创建自定义类来添加文本和值Combobox.

Dictionary对象中添加键和值:

Dictionary<string, string> comboSource = new Dictionary<string, string>();
comboSource.Add("1", "Sunday");
comboSource.Add("2", "Monday");
Run Code Online (Sandbox Code Playgroud)

将源Dictionary对象绑定到Combobox:

comboBox1.DataSource = new BindingSource(comboSource, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
Run Code Online (Sandbox Code Playgroud)

检索键和值:

string key = ((KeyValuePair<string,string>)comboBox1.SelectedItem).Key;
string value = ((KeyValuePair<string,string>)comboBox1.SelectedItem).Value;
Run Code Online (Sandbox Code Playgroud)

完整来源:Combobox文本和价值


Amr*_*rhy 14

这是我想到的方式之一:

combo1.Items.Add(new ListItem("Text", "Value"))

要更改项目的文本或值,您可以这样做:

combo1.Items[0].Text = 'new Text';

combo1.Items[0].Value = 'new Value';
Run Code Online (Sandbox Code Playgroud)

Windows窗体中没有名为ListItem的类.它只存在于ASP.NET中,所以在使用之前你需要编写自己的类,就像@Adam Markowitz在他的回答中所做的那样.

还要检查这些页面,它们可能有所帮助:

  • 除非我弄错了,否则ListItem仅在ASP.NET中可用 (2认同)

小智 11

不知道这是否适用于原帖中给出的情况(更不用说这是两年后的事实),但这个例子对我有用:

Hashtable htImageTypes = new Hashtable();
htImageTypes.Add("JPEG", "*.jpg");
htImageTypes.Add("GIF", "*.gif");
htImageTypes.Add("BMP", "*.bmp");

foreach (DictionaryEntry ImageType in htImageTypes)
{
    cmbImageType.Items.Add(ImageType);
}
cmbImageType.DisplayMember = "key";
cmbImageType.ValueMember = "value";
Run Code Online (Sandbox Code Playgroud)

要重新读取您的值,您必须将SelectedItem属性强制转换为DictionaryEntry对象,然后您可以评估它的Key和Value属性.例如:

DictionaryEntry deImgType = (DictionaryEntry)cmbImageType.SelectedItem;
MessageBox.Show(deImgType.Key + ": " + deImgType.Value);
Run Code Online (Sandbox Code Playgroud)


Rya*_*yan 7

//set 
comboBox1.DisplayMember = "Value"; 
//to add 
comboBox1.Items.Add(new KeyValuePair("2", "This text is displayed")); 
//to access the 'tag' property 
string tag = ((KeyValuePair< string, string >)comboBox1.SelectedItem).Key; 
MessageBox.Show(tag);
Run Code Online (Sandbox Code Playgroud)


Mat*_*cha 5

如果有人对此仍然感兴趣,这里有一个简单而灵活的类,用于带有文本和任何类型值的组合框项(与 Adam Markowitz 的示例非常相似):

public class ComboBoxItem<T>
{
    public string Name;
    public T value = default(T);

    public ComboBoxItem(string Name, T value)
    {
        this.Name = Name;
        this.value = value;
    }

    public override string ToString()
    {
        return Name;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用 the<T>比将 the声明value为 an更好object,因为这样object您就必须跟踪您用于每个项目的类型,并将其转换到您的代码中以正确使用它。

我已经在我的项目中使用它有一段时间了。这真的很方便。