Results 1 to 4 of 4

Thread: Compare listbox items

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    41

    Compare listbox items

    Okay what I am trying to do is compare the listbox items for additional items.

    Example:

    List 1 has the following items on it:

    Test

    Test2

    Test3

    Exampe

    And List2 has the following items on it:

    Test2

    Test4

    Test5

    Test6

    If list2 matches list1 then drop that item, however if it does not match then add the item to a list3 box. This means, if list1 has the same items as list2 then remove all the same items, and add the non same ideas from list2 to list3 making the additional items from list2 show up on list3.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Compare listbox items

    If list2 matches list1 then drop that item, however if it does not match then add the item to a list3 box.
    Hope this helps...

    Code:
    Dim BoolAdd As Boolean, i As Long, j As Long
    Private Sub Command1_Click()
        '~~> Set initial Flag
        BoolAdd = True
        '~~> If list2 is empty then abort operation
        If List2.ListCount = 0 Then
            MsgBox "Nothing to compare"
            Exit Sub
        '~~> if list1 is empty then copy entire list2 to list3
        ElseIf List1.ListCount = 0 Then
            For i = 0 To List2.ListCount
                List3.AddItem List2.List(i)
            Next i
        Else
            For i = List2.ListCount - 1 To 0 Step -1
                For j = 0 To List1.ListCount
                    If List2.List(i) = List1.List(j) Then
                        '~~> If match found then abort
                        BoolAdd = False
                        Exit For
                    End If
                    DoEvents
                Next j
                '~~> If not found then add to list3
                If BoolAdd = True Then List3.AddItem List2.List(i)
                DoEvents
            Next i
        End If
    End Sub
    
    Private Sub Form_Load()
        '~~> Sample Data
        
        '~~> Adding item to list1
        List1.AddItem "Test"
        List1.AddItem "Test2"
        List1.AddItem "Test3"
        
        '~~> Adding item to list2
        List2.AddItem "Test2"
        List2.AddItem "Test4"
        List2.AddItem "Test5"
        List2.AddItem "Test6"
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Compare listbox items

    Depending on how many items your listbox has, you may want to adapt koolsid's example using APIs vs looping thru both listboxes. Faux code looks a little like this & API search is tons faster:
    Code:
    For I = Listbox2.ListCount -1 to Step -1 ' work from bottom up
         If SendMessage(ListBox1.hWnd, LB_FINDSTRINGEXACT, -1, ByVal ListBox2.List(I)) = -1 Then
            ' not found
            ... ListBox3.Add Item ListBox2.List(I), 0
         Else ' found, remove it from #2
            ... ListBox2.RemoveItem I
         End If
    Next
    Last edited by LaVolpe; Mar 21st, 2009 at 04:35 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Compare listbox items

    nice one keith
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width