如何将项目(文本和值)添加到ComboBox&在SelectedIndexChanged中读取它们(SelectedValue = null)

时间:2013-04-03 06:41:43

标签: c# winforms combobox

我想在这样的组合框中添加一些项目:

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总是为null!问题是什么?我该如何解决?

5 个答案:

答案 0 :(得分:7)

您可以将SelectedItem转回class并访问properties

 MessageBox.Show(((ComboboxItem)ComboBox_Countries_In_Silvers.SelectedItem).Value);

编辑您可以尝试使用DataTextField和DataValueField,我将其与DataSource一起使用。

ComboBox_Servers.DataTextField = "Text";
ComboBox_Servers.DataValueField = "Value";

答案 1 :(得分:5)

试试这个:

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

编辑(一点点解释,sory,我也没注意到你的组合框没有被束缚,我责备缺少咖啡因):

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

因此,如果您的组合框未绑定到数据源,则DisplayMember和ValueMember不会执行任何操作,并且SelectedValue将始终为null,则不会调用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());

答案 2 :(得分:4)

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

Aftr adding values to Dictionary, use this as combobox datasource.

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

答案 3 :(得分:2)

这与其他一些答案类似,但是很紧凑,如果您已有列表,则可以避免转换为字典。

鉴于ComboBox&#34;组合框&#34;在Windows表单和具有SomeClass类型属性string的类Name上,

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

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

这意味着combobox.SelectedItem是来自SomeClass的{​​{1}}个对象,list中的每个项目都将使用其属性combobox显示。

您可以使用

阅读所选项目
Name

答案 4 :(得分: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”不起作用,我不想将我的组合框与任何来源绑定,所以我提出了这个解决方案。也许它会帮助别人。