0

I've restored the code and I need your help again. I created 2 Listbox boxes, I entered the data, and now I want to compare the 2 Listboxes and show the differences and similarities in another textbox. Everything I tried does not work at all, it just does not want it. We have the following: And how can this be done on the Listbox line, ie Item (1,2,3,4). if it's possible. Because I will add more data to the listbox, which I want to compare with the other Listbox. Or simpler, how can I compare to the 2nd item on the listbox? Example: Listbox1.Items (1) = 1,2,3,4,5 ListBox1.Items (2) = 1,3,4,5,6

Listbox1.Items - with the following item: 1,3,5,11
Listbox2.Items - with the following item - 2,3,6,11

In Textbox1.text - you want to display the similarities and differences in Textbox2.text, for example the number that exists in Listbox1, and in Listbox2, is the number 3 and the number 11. Then the differences: 1.5,6 in Textbox2.text I realized it was much easier in the Listbox to do these things, so I hope you help me.

The intersection is easier in the listbox so I hope you help me because there is absolutely no code found worked, it's not good, it just does not show me the differences, or the similarities.

  • Hi and welcome to Stack Overflow. Its really important to ask questions properly here. Have a look at [ask] and [mcve]. This site is for questions regarding specific issues with existing code. As it stands, your question is off-topic because you don't explain what behaviour you're getting and what errors you get and on what line they occur. Don't let negative votes or votes to close put you off though. Thanks – David Wilson Jan 02 '19 at 16:53

1 Answers1

0

First, I filled the ListBoxes with your numbers using the .AddRange method.

We could have just started with arrays but I am assuming the ListBoxes were filled somewhere else. Converted the ListBox items to an array.

The Intersection is easy with .Intersect method. The second is a little trickier because .Except returns the elements in the first sequence that don't appear in the second. So, we need to use .Except on both arrays and the combine them with .Union.

Private Sub CompareListBoxes()
    ListBox1.Items.AddRange(New Object() {1, 3, 5, 11})
    ListBox2.Items.AddRange(New Object() {2, 3, 6, 11})
    Dim id1() As Integer = (From i In ListBox1.Items Select CInt(i)).ToArray
    Dim id2() As Integer = (From i In ListBox2.Items Select CInt(i)).ToArray
    Dim Matches As IEnumerable(Of Integer) = id1.Intersect(id2)
    'Known as disjunctive union
    Dim NotMatches As IEnumerable(Of Integer) = id1.Except(id2).Union(id2.Except(id1))
    MessageBox.Show(Matches.Count.ToString)
    'TextBox1 and 2 .Multiline = True is set at design time
    'Expand the text box size so several lines will be visible
    For Each Match As Integer In Matches
        TextBox1.Text &= CStr(Match) & Environment.NewLine
    Next

    For Each NotMatch In NotMatches
        TextBox2.Text &= NotMatch.ToString & Environment.NewLine
    Next
End Sub

The disjunctive union code was courtesy of @Øyvind Bråthen The opposite of Intersect()

The set theory from Wikipedia https://en.wikipedia.org/wiki/Set_theory

EDIT

Just found out about HashSet(Of T). This collection has a method called .SymetricExceptWith that alters the first HashSet to contain only the elements that are not common to both sets. Called disjunctive union or, as the docs call it, symmetric difference. One caveat, cannot contain duplicate elements.

    Dim hs1 As New HashSet(Of Integer) From {1, 3, 5, 11}
    Dim hs2 As New HashSet(Of Integer) From {2, 3, 6, 11}
    hs1.SymmetricExceptWith(hs2)
    For Each i As Integer In hs1
        Debug.Print(i.ToString)
    Next
    'result 1 6 5 2
Mary
  • 14,926
  • 3
  • 18
  • 27