如何向ComboBox中添加项目(文本和值),并在SelectedIndexChanged事件中读取它们(SelectedValue = null)

5
我想向下拉框中添加一些项目,就像这样:
public class ComboboxItem
{
    public string Text { get; set; }
    public object Value { get; set; }

    public override string ToString()
    {
        return Text;
    }
}

ComboboxItem item = new ComboboxItem();
item.Text = "choose a server...";
item.Value = "-1";
ComboBox_Servers.Items.Add(item);
...   

并且可以像这样在 SelectedIndexChanged 中读取那些文本和值:

private void ComboBox_Servers_SelectedIndexChanged(object sender, EventArgs e)
{
    MessageBox.Show(ComboBox_Servers.SelectedValue.ToString());
}

但是SelectedValue始终为空!问题在哪里以及如何修复?

5个回答

8

您可以获取SelectedItem并将其转换回您的class,然后访问其properties

 MessageBox.Show(((ComboboxItem)ComboBox_Countries_In_Silvers.SelectedItem).Value);
编辑:您可以尝试使用DataTextField和DataValueField,我将其与数据源一起使用。
ComboBox_Servers.DataTextField = "Text";
ComboBox_Servers.DataValueField = "Value";

你可以尝试使用类属性设置DataTextField和DataValueField属性,然后访问value属性。 - Adil
更新了我的回答,告诉我它带来了什么? - Adil

6

试试这个:

ComboBox cbx = new ComboBox();
cbx.DisplayMember = "Text";
cbx.ValueMember = "Value";

编辑(稍作解释,抱歉,我也没有注意到您的组合框未被绑定,我把它归咎于缺乏咖啡因):

这里很好地解释了SelectedValue和SelectedItem之间的区别:ComboBox SelectedItem vs SelectedValue

因此,如果您的组合框未绑定数据源,则DisplayMember和ValueMember不起作用,SelectedValue始终为空,SelectedValueChanged不会被调用。因此,请绑定您的组合框:

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

            List<ComboboxItem> list = new List<ComboboxItem>();

            ComboboxItem item = new ComboboxItem();
            item.Text = "choose a server...";
            item.Value = "-1";
            list.Add(item);

            item = new ComboboxItem();
            item.Text = "S1";
            item.Value = "1";
            list.Add(item);

            item = new ComboboxItem();
            item.Text = "S2";
            item.Value = "2";
            list.Add(item);

            cbx.DataSource = list; // bind combobox to a datasource

或者使用SelectedItem属性:
if (cbx.SelectedItem != null)
             Console.WriteLine("ITEM: "+comboBox1.SelectedItem.ToString());

DisplayMember 并不是无用的。当使用非数据绑定的组合框时,DisplayMember 属性确实起作用。这很令人困惑,因为人们也会期望 SelectedValue 起作用。 - comecme

4
Dictionary<int,string> comboSource = new Dictionary<int,string>();
comboSource.Add(1, "Sunday");
comboSource.Add(2, "Monday");

在向 Dictionary 添加值后,将其用作 combobox 数据源:

comboBox1.DataSource = new BindingSource(comboSource, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";

2
这与其他答案类似,但更紧凑且避免了如果您已经有一个列表,则将其转换为字典。
给定窗体上的ComboBox "combobox"和具有字符串类型属性Name的类SomeClass,请使用以下代码:
```csharp List list = new List(); // 添加元素到列表中 combobox.DataSource = list; combobox.DisplayMember = "Name"; ```
以上代码可以将列表中的元素添加到ComboBox中,并将ComboBox的显示文本设置为元素的Name属性。
List<SomeClass> list = new List<SomeClass>();

combobox.DisplayMember = "Name";
combobox.DataSource = list;

这意味着combobox.SelectedItem是来自listSomeClass对象,并且combobox中的每个项目将使用其属性Name进行显示。
您可以使用以下方法读取所选项目。
SomeClass someClass = (SomeClass)combobox.SelectedItem;

0
        combo1.DisplayMember = "Text";
        combo1.ValueMember = "Value";   
        combo1.Items.Add(new { Text = "someText"), Value = "someValue") });
        dynamic item = combo1.Items[combo1.SelectedIndex];
        var itemValue = item.Value;
        var itemText = item.Text;

不幸的是,“combo1.SelectedValue”无法工作,我不想将我的组合框与任何源绑定,所以我想出了这个解决方案。也许它会帮助某些人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接