1

currently i have combobox in my win forms application and, just for test i made a loop so it adds 10 items, but all 10 items in combobox are same!

here is my loop in main class: `

       void AddValue(){
             ComboboxItem item = new ComboboxItem();

        for (int i = 0; i < 10; i++)
        {
            item.Text = "Item " + i;
            item.Value = i;
            ModDown.Items.Add(item);
        }
       }

and ComboboxItem class: `

    class ComboboxItem
      {

    public string Text { get; set; }
    public int Value { get; set; }

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

thanks for any help!

  • Nick.
Nick
  • 455
  • 9
  • 28

1 Answers1

4

You keep adding the same ComboBoxItem to the combobox. You just change its property consequently.

void AddValue()
{
for(int i = 0; i < 10; i++)
    {
    ComboboxItem item = new ComboboxItem();
    item.Text = "Item " + i;
    item.Value = i;
    ModDown.Items.Add(item);
    }
}
Dartek12
  • 172
  • 1
  • 2
  • 12
  • :D haha didn't notice that thanks anyways! but what about getting value of it. for eg: when i press button messagebox shows up and it should write Combobox1.SelectedValue, but i get: null – Nick Aug 05 '16 at 08:47
  • 1
    Get a look over there: http://stackoverflow.com/questions/6901070/getting-selected-value-of-a-combobox – Dartek12 Aug 05 '16 at 08:56
  • Thank you! you really helped me out! – Nick Aug 05 '16 at 09:05