+ 1

How can I compare two or more listboxes?

I have two listboxes containing a couple of elements and I would like to find out if any of these elements can be found in both boxes - it shouldn't matter if they have the same list index or not. I'm working with visual studio 2017

29th Aug 2017, 3:45 PM
Fabian Köninger
Fabian Köninger - avatar
2 odpowiedzi
0
string FirstString, SecondString; for (int x=0; x < listBox1.Items.Count;x++) { //loop through first listbox //MessageBox.Show(listBox1.Items[x].ToString()); FirstString = listBox1.Items[x].ToString(); for (int y = 0; y < listBox2.Items.Count; y++) { //loop through second listbox //MessageBox.Show(listBox1.Items[y].ToString()); SecondString = listBox2.Items[y].ToString(); if (FirstString == SecondString) //compare values { //if equal place result in listbox3 listBox3.Items.Add(FirstString); } } } for (int x = 0; x < listBox1.Items.Count; x++) { //loop through firstlistbox if (listBox2.Items.Contains(listBox1.Items[x])) {//use Contains method to find similar object, if equal place in listbox3 listBox3.Items.Add(listBox1.Items[x].ToString()); } Hope this helps, play around with it and have fun }
29th Aug 2017, 6:52 PM
sneeze
sneeze - avatar
0
Thanks so much for the quick answer, it was bugging me all day long :)
29th Aug 2017, 8:58 PM
Fabian Köninger
Fabian Köninger - avatar