1

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;
    }
}
Michael Haddad
  • 4,085
  • 7
  • 42
  • 82
user3249619
  • 13
  • 1
  • 1
  • 3

4 Answers4

1

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));
    }
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
0

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

so add it as :

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

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)

Community
  • 1
  • 1
JEV
  • 2,494
  • 4
  • 33
  • 47
0

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);
Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
  • Could of just linked your answer to here? http://stackoverflow.com/questions/3063320/combobox-adding-text-and-value-to-an-item-no-binding-source – JEV Jan 29 '14 at 16:25