4 Attachment(s)
How do I compare the items of two listboxes?
I have two listboxes (1: Primary, 2:Secondary). These listboxes contain numbers. The Primary Listbox contains 7 numbers, and the Secondary Listbox contains 6 numbers.
Attachment 160289
I want to compare the values of the Primary Listbox to those of the Secondary. This comparison should yield three results:
Result #1: X number of values were found to be common.
Attachment 160291
Result#2: All numbers matched.
Attachment 160293
Result#3: No matches found.
Attachment 160295
This is what I have so far:
Code:
If lstPrimaryNumbers.Items.Count = 0 Or lstSecondaryNumbers.Items.Count = 0 Then
MessageBox.Show("There is nothing to compare.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Dim r = lstPrimaryNumbers.Items.Cast(Of String).Where(Function(x) lstSecondaryNumbers.Items.Contains(x))
MessageBox.Show(String.Join(",", r) & " matched")
Note: I`m only dealing with integer values in the two listboxes.
Re: How do I compare the items of two listboxes?
After you fill the r variable, check how many items are in it... if it is 0 then there are "No matches", if it is equal to the count of items in whichever listbox(es) you want for a "full match" then it is a full match, otherwise show what you are showing.
Re: How do I compare the items of two listboxes?
If I want to set the r = 0 in an if-statement for example, it cannot be done, since the variable contains IEnumerable interface.
Re: How do I compare the items of two listboxes?
You shouldn't be checking r itself, but the number of elements in it... so a .Count method might be useful:
https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
Re: How do I compare the items of two listboxes?
As you say, there are three possible outcomes. That sounds like a job for... Select Case. Before we get tot hat though, what does "all numbers matched" actually mean in this case? As you said, there are 7 item sin one list and 6 in the other so, unless you're allowing duplicates, all the numbers in the first list can't possibly be in the second. Also, if there were 6 items in the first list and 7 in the second then it would be possible that every item in the first was in the second but does that count as every item matching because there must be an item in the second list not in the first. You need to define your problem FULLY. If you only define it partially then you can only code it partially and you'll get unexpected results in some instances. For the purposes of this example, I'm going to assume no duplicates and all items from the first list contained in the seconds is considered a complete match:
vb.net Code:
Dim matchingItems = lstPrimaryNumbers.Items.
Cast(Of String)().
Where(Function(x) lstSecondaryNumbers.Items.Contains(x)).
ToArray()
Select Case matchingItems.Length
Case 0:
'No match
Case lstPrimaryNumbers.Items.Count:
'Complete match
Case Else:
'Partial match
End Select
Notice the meaningful variable name and also the ToArray call. If you didn't call ToArray then you'd be left with a pending query and you would potentially have to enumerate it twice, i.e. once to get the Count of the items and again to get the items themselves in the case of a partial match. Enumerating the results of a query multiple times is always bad so, if it would or could be required, call ToArray or ToList on the query and then use that multiple times.
Re: How do I compare the items of two listboxes?
Hi Me.User,
here a few samples, you can extend the "CompareListboxes" Function to your needs
add a Button and 3 Listboxes to a Form
Code:
Option Strict On
Public Class Form2
Private Enum SelectionCriteria
IsInBoth
IsInFirstOnly
IsInSecondOnly
End Enum
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim l1 As New ListBox
l1.Items.Add("3")
l1.Items.Add("25")
l1.Items.Add("36")
l1.Items.Add("38")
l1.Items.Add("39")
l1.Items.Add("40")
l1.Items.Add("17")
Dim l2 As New ListBox
l2.Items.Add("4")
l2.Items.Add("13")
l2.Items.Add("25")
l2.Items.Add("33")
l2.Items.Add("40")
l2.Items.Add("42")
'show results in Listbox 1 to 3
Dim a() As Object = CType(CompareListboxes(l1, l2, SelectionCriteria.IsInBoth), Object())
ListBox1.DataSource = a
Dim b() As Object = CType(CompareListboxes(l1, l2, SelectionCriteria.IsInFirstOnly), Object())
ListBox2.DataSource = b
Dim c() As Object = CType(CompareListboxes(l1, l2, SelectionCriteria.IsInSecondOnly), Object())
ListBox3.DataSource = c
End Sub
Private Function CompareListboxes(ByVal l1 As ListBox, ByVal l2 As ListBox, ByVal sel As SelectionCriteria) As Array
Select Case sel
Case SelectionCriteria.IsInBoth
Return (From l In l1.Items Join m In l2.Items On l Equals m Select l).ToArray
Case SelectionCriteria.IsInFirstOnly
Return (From l In l1.Items Where Not (From m In l2.Items Select m).Contains(l) Select l).ToArray
Case SelectionCriteria.IsInSecondOnly
Return (From l In l2.Items Where Not (From m In l1.Items Select m).Contains(l) Select l).ToArray
Case Else
Return Nothing
End Select
End Function
End Class
regards
Chris
Re: How do I compare the items of two listboxes?
This seems to be returning the first case only, no match. I tried it out, and visually inspected for a match. I found one match, I ran the code, it returned no matches. I`ll look into it more.
Re: How do I compare the items of two listboxes?
Did you debug the code, i.e. set a breakpoint and step through it?
Re: How do I compare the items of two listboxes?
si_the_geek Can you please delete this thread? It`s obsolete, I have no use for it anymore since I changed my entire program`s schema. Thanks.
Re: How do I compare the items of two listboxes?
We don't delete threads, as the information in them may still be useful for others. Lots of people find answers by searching, and it is easily possible that this thread answers some questions that people have in future.
If you don't need help with this any more, the best thing to do is mark it as Resolved (via the Thread Tools menu at the top of the page). That way people wont spend time trying to help when it is no longer useful to you.