简体   繁体   中英

c# ComboBox, save item with value and text

I am trying to add an object ITEM with TEXT and VALUE to a ComboBox so I can read it later

public partial class Form1 : Form
{
    ComboboxItem item;
    public Form1()
    {
        InitializeComponent();

        comboBox1.Items.Add(new ComboboxItem("Dormir", 12).Text);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ComboboxItem c = (ComboboxItem)comboBox1.SelectedItem;
        label1.Text = c.Text;
        label2.Text = c.Value.ToString();
    }
}

The problem is, I cant add the full Item because isn't a string...and give an exception at beginning of click event


Extra information: This ComboboxItem, its a class that I created with 2 parameters, string, and int

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

    public ComboboxItem(string texto, double valor)
    {
        this.Text = texto;
        this.Value = valor;
    }
}

you dont need to add .Text at the end of ("text","value")

so add it as :

comboBox1.Items.Add(new ComboBoxItem("dormir","12"));

You are on the right lines by creating your own ComboBoxItem class.

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

}

There are two ways to use this, (Constructor aside): Method 1:

private void button1_Click(object sender, EventArgs e)
{
    ComboboxItem item = new ComboboxItem();
    item.Text = "Item text1";
    item.Value = 12;

    comboBox1.Items.Add(item);
}

Method 2:

private void button1_Click(object sender, EventArgs e)
{
    ComboboxItem item = new ComboboxItem 
    { 
         Text = "Item text1",
         Value = 12
    };

    comboBox1.Items.Add(item);
}

Have you tried adding it like this instead? Then when ever you get the Item out just cast it as a ComboboxItem :)

...
var selectedItem = comboBox1.SelectedItem as ComboboxItem;
var myValue = selectedItem.Value;
...

Alternative KeyValuePair:

comboBox1.Items.Add(new KeyValuePair("Item1", "Item1 Value"));

Question based on returnign string value and other combobox answers.. ComboBox: Adding Text and Value to an Item (no Binding Source)

You could (should) set the displaymember and valuemember in another place, but...

public Form1()
    {
        InitializeComponent();
        comboBox1.DisplayMember="Text";
        comboBox1.ValueMember ="Value";
        comboBox1.Items.Add(new ComboboxItem("Dormir", 12));
    }

Create the ComboboxItem class and override the ToString method. The ToString method will be called to visualize the item. By default, ToString() returns the typename.

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

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

Then, you can do this:

var item = new CombobxItem { Value = 123, Text = "Some text" };
combobox1.Items.Add(item);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM