简体   繁体   中英

ComboBox items using loop

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.

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);
    }
}

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