I am starting to feel completely dumb when it comes to VB.Net coding. I am using Visual Studio 2010 and am trying to compare the numbers in 2 listboxes. If an item from list A matches an item from list B, I want it to add that item to List C. Once it has added the item to List C, remove those searched items from list A & B. Continue until it runs out of numbers to check.

If you are wondering what I am trying to do, I want to make a Common Factor list. I already have the Factoring list done, but got stuck here.

So far, my code looks like

Private Sub CommonFactors(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Common.Click
        Dim BoolAdd As Boolean, i As Double, j As Double
        BoolAdd = False
        ' lstFact1 & lstFact2 are the factor listboxes
        ' lstCommon is where the common factors are supposed to go
        If lstFact1.Items.Count = 0 Then
            Exit Sub
        ElseIf lstFact2.Items.Count = 0 Then
            Exit Sub
        Else
            For i = lstFact1.Items.Count - 1 To 0 Step -1
                For j = 0 To lstFact2.Items.Count
                    If lstFact1.Items(i) = lstFact2.Items(j) Then
                        BoolAdd = True
                        Exit For
                    Else
                        BoolAdd = False
                    End If
                Next j

                If BoolAdd = True Then
                    lstCommon.Items.Add(lstFact1.Items(i))
                    lstFact1.Items.Remove(i)
                    lstFact2.Items.Remove(j)
                End If
            Next i
        End If
    End Sub

Thanks in advance for any help you can provide.

For j = 0 To lstFact2.Items.Count

maybe better

for 0 To lstFact2.Items.Count - 0 ?

just thinking.

If you remove an item from the lstFact1, it affects the items count in the listbox which you are skipping in the opposite direction

maybe leave lstfact1 en lstfact2 alone

let's see how that works and I will give a comment

Results
I used your comment and actually got an error when I removed the j=0 but it worked adding the Count - 0. Only thing that I don't like is that it lists the Common Factors from highest to lowest. I am currently looking up how to sort a listbox from lowest value to highest value.

Edited 5 Years Ago by tstory28: Added results

Hi,

I've one more question:
What will you do if the number doesn't match in listbox2?

If they do not match, it keeps BoolAdd = False which means it does not enter it in lstCommon.

Edited 5 Years Ago by tstory28: n/a

Ok. I see what you mean by not matching. I just tried to fix that issue but it is still giving me an error. Here is how I tried to fix it, but unsuccessfully.

Private Sub CommonFactors(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Common.Click
        Dim BoolAdd As Boolean, i As Double, j As Double
        BoolAdd = True
        If lstFact1.Items.Count = 0 Then
            Exit Sub
        ElseIf lstFact2.Items.Count = 0 Then
            Exit Sub
        Else
            For i = lstFact1.Items.Count - 1 To 0 Step -1
                For j = 0 To lstFact2.Items.Count - 0
                    If lstFact1.Items(i) < lstFact2.Items(j) Then
                        BoolAdd = False
                    ElseIf lstFact1.Items(i) > lstFact2.Items(j) Then
                        BoolAdd = False
                    ElseIf lstFact1.Items(i) = lstFact2.Items(j) Then
                        Exit For
                    End If
                Next j

                If BoolAdd = True Then
                    lstCommon.Items.Add(lstFact1.Items(i))
                End If
            Next i
        End If

    End Sub

Any suggestions???

Dim CheckString As String
For i = ListBox1.Items.Count - 1 To 0 Step -1
    CheckString = CStr(ListBox1.Items(i))
    Do While ListBox2.Items.Contains(CheckString)
       ListBox2.Items.Remove(CheckString)
       If Not ListBox3.Items.Contains(CheckString) Then
            ListBox3.Items.Add(CheckString)
       End If
    Loop
    ListBox1.Items.RemoveAt(i)
Next

That worked somewhat except for the fact when I use the numbers 700 and 7000 it created an error.

Factors of 700 - 2, 2, 5, 5, 7
Factors of 7000 - 2, 2, 2, 5, 5, 5, 7

When I do the operation for Common Factors, it only input 7 and 5, instead of 2,2,5,5,7.

The code is good except it don't allow for duplications. I have been trying to figure that out at this point but still lost.

Edited 5 Years Ago by tstory28: n/a

????
Not a clue what your talking about.

Your post said compare numbers in 2 listboxes and remove them, add the mathces in a 3rd. Pretty sure it does it.

You will have to explain a little more.

Factors of 700 - 2,2,5,5,7 Have no clue what thats supposed to mean.

If you want all the evenly divisble numbers of a number then you can use this.

Dim Factors As New List(Of Integer)
For i = 700 To 1 Step -1
     If 700 Mod i = 0 Then
          Factors.Add(i)
     End If
Next

And the above code doesn't add dublicates in the third listbox. If you want duplicates remove the If not listbox3.contains and just add it to the 3rd list box.

But please provide the error you got.

Show the numbers you had in the 1st and 2nd list box so I can produce the same error.

Edited 5 Years Ago by Unhnd_Exception: n/a

I am in the process of making an algebraic calculator. I am currently working on a Least Common Denominator function. That is why I have the comparison tables going. I got buttons that do all the prime numbers and factors needed. Last thing I have to get working properly is comparing the factors of listbox1 and listbox2. When comparing the two listboxes, there are possibilities of duplicate numbers.

Listbox1 would contain 2, 2, 5, 5, 7
Listbox2 would contain 2, 2, 2, 5, 5, 5, 7

What I need to actually happen is the common numbers (2, 2, 5, 5, 7) to be added to Listbox3.

With the current code, all that is added to Listbox3 is 2, 5, and 7.

Here this will add duplicates.

Dim CheckString As String
For i = ListBox1.Items.Count - 1 To 0 Step -1
    CheckString = CStr(ListBox1.Items(i))
    If ListBox2.Items.Contains(CheckString) Then
        ListBox2.Items.Remove(CheckString)
        ListBox3.Items.Add(CheckString)
    End If
    ListBox1.Items.RemoveAt(i)
Next

If you want the third listbox from lowest to highest then you can work backwards on listbox1's items or set the ListBox3.Sort = true on the designer.

Don't forget to mark the thread as solved if it solves your situation.

This question has already been answered. Start a new discussion instead.