Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Compare Strings in Two Listboxes

 

Ok, here's my situation:

 

I have 2 listboxes that I am trying to compare the contents of.

So, for eachline in listbox2, I need to see if the that line is also in listbox1. The code in a function I have tried to write so far is not working and I don't understand exactly why.

 


Function FoundMatch() As Boolean

       Dim j, i As Integer
       For j = 0 To ListBox2.Items.Count - 1
           For i = 0 To ListBox1.Items.Count - 1
               If ListBox1.Items.Item(i) = ListBox2.Items.Item(j) Then

                ' Return True
                 MsgBox("match")
               Else
              
               End If
           Next i
       Next j

   End Function



 

Can anyone help me understand what I am doing wrong?

 

Thanks in advance!

Edited by micropathic
  • Leaders
Posted

try this ...

       Dim s As String
       For Each s In ListBox1.Items
           If Not ListBox2.Items.IndexOf(s) = -1 Then '/// if it's also indexed in listbox2
               ListBox2.Items.RemoveAt(ListBox2.Items.IndexOf(s))
           End If
       Next

Posted

Thanks to both of you for your help! I was able to make it work using a combination of the code the code you provided:

 




Function FoundMatch() As Boolean

    Dim s As String
       For Each s In ListBox1.Items
           If Not ListBox2.Items.IndexOf(s) = -1 Then '/// if it's also indexed in listbox2
               ListBox2.Items.RemoveAt(ListBox2.Items.IndexOf(s))
           End If
       Next

End Function

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...