micropathic Posted January 24, 2004 Posted January 24, 2004 (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 January 25, 2004 by micropathic Quote
Leaders dynamic_sysop Posted January 24, 2004 Leaders Posted January 24, 2004 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 Quote
Administrators PlausiblyDamp Posted January 24, 2004 Administrators Posted January 24, 2004 try Public Function FoundMatch() As Boolean For Each s As String In ListBox1.Items If ListBox2.Items.IndexOf(s) > 0 Then Return True End If Next Return False End Function Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
micropathic Posted January 25, 2004 Author Posted January 25, 2004 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.