Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare two listbox 1

Status
Not open for further replies.

unixisgood

Technical User
Jan 26, 2005
25
GB
Hi All,
I want to compare contents of two listbox and find same data between list1 and list2 and then move only same data to list3.
Is this possible?
 
Not enough info of what you want to do.

But you can start with:
Code:
Dim intL1 As Integer
Dim intL2 As Integer

For intL1 = 0 To List1.ListCount - 1
    For intL2 = 0 To List2.ListCount - 1
        ... your code here
    Next intL2
Next intL1


Have fun.

---- Andy
 
Or if you're willing to use a listview as the second control to cut out one loop:
Code:
Dim i As Integer
Dim lstItm As ListItem

For i = 0 To List1.ListCount - 1
    Set lstItm = ListView2.FindItem(List1.List(i), lvwText, , lvwWholeWord)

        If Not lstItm Is Nothing Then
            List3.AddItem lstItm.Text
        End If
    
Next i
Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Hi Andrzejek ,
Thanks for answer.
I ll try to explain it more.

I have 3 listbox on my form. i need to compare list1 and list2 to find same values in them and add the same values to list3 as like below example.


List1 List2 List3
1 8 1
2 7 4
3 2
4 1
5 4


Kind Regards
 
Dim L1 As Integer, L2 As Integer

For L1 = List1.ListCount - 1 To 0 Step -1
For L2 = List2.ListCount - 1 To 0 Step -1
If List1.List(L1) = List2.List(L2) Then
List3.AddItem List1.List(L1)
List1.RemoveItem L1
List2.RemoveItem L2
Exit For
End If
Next L2
Next L1

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Hi HarleyQuinn ,
Thanks for help but it gives error like `type mismatch`
 
Just out of interest, where in the code did you get the type mismatch?

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Hi HarleyQuinn
I get this error on `Set lstItm = ListView2.FindItem(List1.List(i), lvwText, , lvwWholeWord)`

I am not so good on VB so maybe i am causing to get this error.
 
I can get the same error if i use i as a string variable or use the literal "i" in the index parameter but apart from that I can't duplicate the error.

Anyway, if you've got it working with the listboxes (as per your original requirement) then there's probably no need for you to pursue the ListView option [smile]

Cheers

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
It is a type, I hope, because:
Code:
List1     List2    List3
1         8        1
[blue]2[/blue]         7        4
3         [blue]2[/blue]        [blue]2 ???[/blue] 
4         1
5         4

Unless there is more to your logic......

Have fun.

---- Andy
 
You can achieve the reduced loop count shown by HarleyQuinn for for Listboxes if you are prepared to use an API call >here's an example that stricks as close as possible to HarleyQUinn's structure, and also includes Error7's added functionality that removes matched itms from Listboxes 1 and 2):
Code:
[blue]Option Explicit

Private Const LB_FINDSTRINGEXACT = &H1A2
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Sub Command1_Click()
    Dim lp As Long
    Dim lstItm As Long
    
    ' Single loop version
    For lp = List1.ListCount - 1 To 0 Step -1
        lstItm = SendMessage(List2.hwnd, LB_FINDSTRINGEXACT, 0&, ByVal List1.List(lp))
        If lstItm <> -1 Then
            List3.AddItem List1.List(lp)
            ' Include the next two lines if you want the same effect as Error7s version
            ' List1.RemoveItem (lp)
            ' List2.RemoveItem (lstItm)
        End If
    Next
End Sub[/blue]

 
strongm strikes again!

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Nice strongm [smile]

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top