0

Is it possible to compare two listboxes and their respected selected values?

Basically I want to check if listbox A's selected values == listbox B's selected values.

I tried this code, but it didn't work:

if(listA.SelectedItems == listB.SelectedItems)
{
    Console.WriteLine("equal");
}
else
{
    Console.WriteLine("not equal");
}
Zach
  • 143
  • 3
  • 13
  • What is the object inside listbox? string or some other class? Also, Is the sequence important? – Sriram Sakthivel Aug 22 '14 at 06:17
  • Both listboxes contain the same data. For example, listbox A contains values `A, B, C, D, E` and likewise for listbox B...and in that order for both boxes. So, if A, B and C are selected in box A and likewise for B, the statement would return true. If box A selected was A and C, while box B was A, D, it would return false. – Zach Aug 22 '14 at 06:20
  • possible duplicate of [Does .NET have a way to check if List a contains all items in List b?](http://stackoverflow.com/questions/1520642/does-net-have-a-way-to-check-if-list-a-contains-all-items-in-list-b) – Sayse Aug 22 '14 at 06:20

3 Answers3

2

You can sort both the SelectedItems collection and then use SequenceEqual.

var orderedA = listA.SelectedItems.Cast<object>().OrderBy(x=> x);  
var orderedB = listB.SelectedItems.Cast<object>().OrderBy(x=> x);
if(orderedA.SequenceEqual(orderedB))
{
    Console.WriteLine("equal");
}
else
{
    Console.WriteLine("not equal");
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
0

The simplest way would be to check if any of the second list contains the items from the first

var listAItems = listA.SelectedItems
var listBItems = listB.SelectedItems
if(listAItems.Count == listBItems.Count &&
   listAItems.Any(i => !listBItems.Contains(i)))
    //Something missing
else
    //All there

Note: this works for all IEnumerables

I'm not sure if this answer would be more efficient for your usage than the one in the duplicate since this will return true as soon as it finds an entry that doesn't exist - The duplicates answer has the possibility to return the items that are missing

var missing = listA.SelectedItems.Except(listB.SelectedItems);
if(missing.Any())
    //something missing use the missing variable to see what
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Your answer checks whether `listA.SelectedItems` contains any items which is not there in `listB`. but OP asks for equals. Your answer will return true when listA contains [`A`, `B`] and listB [`A`, `B`, `C`]. I believe op needs to return false in this case – Sriram Sakthivel Aug 22 '14 at 06:33
  • @SriramSakthivel - Thanks I've updated to cater for that, although I admit this still assumes that the list has no duplicates – Sayse Aug 22 '14 at 06:37
0

You are using two properties with a different meaning for your comparison. SelectedItem is an object (could be anything depending on how you have filled the combo, ValueMember is just the name of a property to use as the actual value for the items in the ListBox.

However the two classes (ListBox and ComboBox) share the same pattern for storing their list items, so supposing that both are populated using a list of strings then your code could be

dynamic curComboItem = ComboBox1.SelectedItem.ToString();
for (i = 0; i <= ListBox1.Items.Count - 1; i++) {
    if (curComboItem == ListBox1.Items(i).ToString()) {
        Interaction.MsgBox("DATA FOUND");
        break; // TODO: might not be correct. Was : Exit For
    }
}
chevhfghfghfgh
  • 127
  • 1
  • 6