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"); } 
3
  • What is the object inside listbox? string or some other class? Also, Is the sequence important? Commented Aug 22, 2014 at 6: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. Commented Aug 22, 2014 at 6:20
  • possible duplicate of Does .NET have a way to check if List a contains all items in List b? Commented Aug 22, 2014 at 6:20

3 Answers 3

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"); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Simple and easy to understand. Thanks!
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 } } 

Comments

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 

2 Comments

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
@SriramSakthivel - Thanks I've updated to cater for that, although I admit this still assumes that the list has no duplicates

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.