简体   繁体   中英

Why when adding items to combobox it's adding the same item many times?

In the top of form1:

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

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

        List<string> results = new List<string>();

Then:

ComboboxItem item = new ComboboxItem();
var result = videoCatagories.Execute();

for (int i = 0; i < result.Items.Count; i++)
            {
                item.Text = result.Items[i].Snippet.Title;
                item.Value = result.Items[i].Id;
                comboBox1.Items.Add(item);
            }

And in the end:

private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            MessageBox.Show((comboBox1.SelectedItem as ComboboxItem).Value.ToString());
        }

What i wanted to do in general is to add to the combobox the titles and then when i select a title to get the title id. For example i run the program and select the title Weather now i want to see in a messageBow.Show the id 1

There are 31 items. When i use a breakpoint and look on result i see 31 items when i click on the first item in index 0 i see Id = "1" and then i click on snippet and see title "weather"

Then i do the same for item in index 1 and i see Id = "19" and in the snippet the title is "animals".

But for some reason it's adding each itertion the same item many times.

Create a new instance of ComboboxItem each time you want to add a new item to the combo box:

for (int i = 0; i < result.Items.Count; i++)
{
    ComboboxItem item = new ComboboxItem();
    item.Text = result.Items[i].Snippet.Title;
    item.Value = result.Items[i].Id;
    comboBox1.Items.Add(item);
}

Your code changes the properties of the same item instance for each entry in results, then adds it to the comboBox1.Items collection. Add inserts its argument to the Items collection, it doesn't copy its contents. As a result, when the combobox is rendered, all combobox items point to the same item. To avoid this, create a new item instance for each entry in results :

for (int i = 0; i < result.Items.Count; i++)
{
    var item=new ComboboxItem 
             {                 
                   Text = result.Items[i].Snippet.Title,
                   Value = result.Items[i].Id
             };
    comboBox1.Items.Add(item);
}

or

var items=from item in result
          select new ComboboxItem 
             {                 
                   Text = item.Snippet.Title,
                   Value = item.Id
             };
comboBox1.Items.AddRange(items);

You could do a simple check to make sure that the combobox doesn't already contain it before doing the insert.

ComboboxItem item = new ComboboxItem();
var result = videoCatagories.Execute();

for (int i = 0; i < result.Items.Count - 1; i++)
{
    if(!comboBox1.Items.Contains(item))
    {
            item.Text = result.Items[i].Snippet.Title;
            item.Value = result.Items[i].Id;
            comboBox1.Items.Add(item);
    }
}

Or you can do like this article suggests and remove every item that is the same before adding the new item to remove conflicts, No Duplicate in a Listbox or using the same way to stop duplicates from a combobox too

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