简体   繁体   English

为什么在向组合框添加项目时会多次添加同一项目?

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

In the top of form1: 在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. 通常,我想做的是将标题添加到组合框中,然后在我选择标题以获取标题ID时。 For example i run the program and select the title Weather now i want to see in a messageBow.Show the id 1 例如我运行程序并选择标题想现在在messageBow中显示的天气。显示id 1

There are 31 items. 有31个项目。 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" 当我使用断点并查看结果时,当我单击索引0中的第一个项目时,我会看到31个项目,我看到Id =“ 1”,然后单击摘要并看到标题“天气”

Then i do the same for item in index 1 and i see Id = "19" and in the snippet the title is "animals". 然后,我对索引1中的项目执行相同操作,我看到ID =“ 19”,并且在代码段中标题为“动物”。

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: 每次要将新项目添加到组合框中时,都要创建一个ComboboxItem的新实例:

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. 您的代码为结果中的每个条目更改同一项目实例的属性,然后将其添加到comboBox1.Items集合。 Add inserts its argument to the Items collection, it doesn't copy its contents. Add将其参数插入Items集合,而不复制其内容。 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 : 为了避免这种情况,请为results每个条目创建一个新的item实例:

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 或者您可以按照本文的建议进行操作,并在添加新项目以删除冲突之前删除所有相同的项目,以消除列表框中的重复项,或者也可以使用相同的方式从组合框中停止重复项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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