PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
How do I compare the items of two listboxes?-VBForums
Results 1 to 10 of 10
  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2017
    Posts
    33

    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.

    Name:  Capture#1.JPG
Views: 328
Size:  15.4 KB

    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.

    Name:  Result#1.JPG
Views: 323
Size:  12.0 KB

    Result#2: All numbers matched.

    Name:  Result#2.JPG
Views: 319
Size:  10.7 KB

    Result#3: No matches found.

    Name:  Result#3.JPG
Views: 319
Size:  11.6 KB

    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.
    Last edited by Me.User; Jul 13th, 2018 at 10:54 AM. Reason: Added attachment

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,938

    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2017
    Posts
    33

    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.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,938

    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

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,181

    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:
    1. Dim matchingItems = lstPrimaryNumbers.Items.
    2.                                       Cast(Of String)().
    3.                                       Where(Function(x) lstSecondaryNumbers.Items.Contains(x)).
    4.                                       ToArray()
    5.  
    6. Select Case matchingItems.Length
    7.     Case 0:
    8.         'No match
    9.     Case lstPrimaryNumbers.Items.Count:
    10.         'Complete match
    11.     Case Else:
    12.         'Partial match
    13. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,089

    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
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2017
    Posts
    33

    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.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,181

    Re: How do I compare the items of two listboxes?

    Did you debug the code, i.e. set a breakpoint and step through it?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2017
    Posts
    33

    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.

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,938

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width