简体   繁体   English

C#两个列表框和两个列表

[英]C# two listboxes and two lists

I'm trying to get two listboxes to work alongside one another. 我正在尝试使两个列表框相互配合工作。 I'm using Listbox1, Listbox2, List1 and List2 (for this example), the two lists are for further purposes so they need to be included. 我使用的是Listbox1,Listbox2,List1和List2(在此示例中),这两个列表用于其他目的,因此需要将它们包括在内。

Listbox2 should contain everything of List2, listbox1 should contain everything of List1 except for the items in List2. Listbox2应该包含List2的所有内容,listbox1应该包含List1的所有内容,除了List2中的项目。 I've been trying alot of different ways, but i just can't seem to get through. 我一直在尝试许多不同的方法,但是似乎无法克服。 Currently Listbox2 gets filled and updated perfectly, but i can't seem to get Listbox1 work accordingly. 目前,Listbox2已得到填充和完美更新,但我似乎无法让Listbox1相应地工作。

Options i've tried (might have written it wrongly) : - add everything of List1 to Listbox1 and then remove everything List2 contains - loop through every List1 item and check if it existed in List2. 我尝试过的选项(可能写错了):-将List1的所有内容添加到Listbox1,然后删除List2包含的所有内容-遍历每个List1项并检查它是否存在于List2中。 Add only if it diden't. 仅在没有添加时添加。

Thanks in advance! 提前致谢!

I know i'm adding doubles here. 我知道我要在这里加倍。 I've tried alot of things and removed everything when trying something new (don't ask why, bad habit) 我尝试了很多事情,尝试了新事物时删除了所有内容(不要问为什么,坏习惯)

(AddToListbox is a working method.) (AddToListbox是一种有效的方法。)

if (classroom.members.Count <= 0)
        {
            foreach (Student student in repository.students)
            {
                AddtoListbox(Listbox1, student);
            }
        }
        else
        {
            foreach (Student student in repository.students)
            {
                foreach (Student studentInClass in classroom.members)
                {
                    if (student.LastName != studentInClass.LastName || student.FirstName != studentInClass.FirstName)
                    {
                        AddtoListbox(Listbox1, student);
                    }
                }
            }
        }

尝试对List1 除外

listbox1.AddRange(List1.Except(List2).Cast<object>.ToArray());

I made a little sample waiting on your code. 我在等待您的代码时做了一些示例。 I have a button that populates two List<int> . 我有一个填充两个List<int>的按钮。 The First list is 1-20, while the second is 5-9. 第一个列表是1-20,第二个列表是5-9。 You will see that it populates Listbox1 with 5-9 and Listbox2 with 1-4 and 10-20. 您将看到它用5-9填充Listbox1 ,并用1-4和10-20填充Listbox2

private void button1_Click(object sender, EventArgs e)
{
     list1 = new List<int>(Enumerable.Range(5,5));
     list2 = new List<int>(Enumerable.Range(1,20));

     foreach (int num1 in list1)
     {
         listBox1.Items.Add(num1);
     }

     foreach (int num2 in list2)
     {
         bool numFound = false;
         foreach (int num1 in list1)
         {
             if (num2 == num1)
             {
                 numFound = true;
                 break;
             }
         }
         if (!numFound)
             listBox2.Items.Add(num2);
     }
}

Not the most eloquent code, but it gets the job done. 不是最雄辩的代码,但是它可以完成工作。

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

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