Results 1 to 13 of 13

Thread: Checking a Listbox for duplicate Items

  1. #1

    Thread Starter
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Checking a Listbox for duplicate Items

    Alright, I was not sure how to really do this. What I want to do, is run through a listbox and check if there are any duplicate items in the box. As in more then one of the exact same item in the listbox. I know how to run through it and like the basic structure, but the checking has got me stumped.
    Age - 15 ::: Level - Advanced
    If you find my post useful please ::Rate It::


  2. #2

  3. #3
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Checking a Listbox for duplicate Items

    VB Code:
    1. Private Function ChkLst(iList As ListBox)
    2.     Dim i As Integer, x As Integer
    3.         For i = 0 To iList.ListCount - 1
    4.             For x = 0 To iList.ListCount - 1
    5.                 If (iList.List(i) = iList.List(x)) And x <> i Then
    6.                     List1.RemoveItem (i)
    7.                 End If
    8.             Next x
    9.         Next i
    10.         MsgBox iList.ListCount
    11. End Function
    12.  
    13. Private Sub Command1_Click()
    14.     Call ChkLst(List1)
    15. End Sub

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Checking a Listbox for duplicate Items

    I always wonder why you would want do that when you don't have to. Just add this code and you won't have duplicates to get rid of.

    VB Code:
    1. Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" _
    2.                       (ByVal hwnd As Long, _
    3.                        ByVal wMsg As Long, _
    4.                        ByVal wParam As Long, _
    5.                        ByVal lParam As String) As Long
    6. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    7.                        (ByVal hwnd As Long, _
    8.                         ByVal wMsg As Long, _
    9.                         ByVal wParam As Long, _
    10.                         lParam As Any) As Long
    11.  
    12. ' In a sub
    13.     Dim lngRetVal As Long
    14.  
    15.     lngRetVal = SendMessageString(MyListbox.hwnd, _
    16.                                   LB_FINDSTRINGEXACT, -1&, _
    17.                                   strStringToBeAdded)
    18.     ' lngRetVal is the ListIndex
    19.     If lngRetVal > -1& Then
    20.         ' It's in the list
    21.     Else
    22.         ' It's not in the list so add it
    23.         Call SendMessage(MyListbox.hwnd, LB_ADDSTRING, 0, ByVal strStringToBeAdded)
    24.     End If

  5. #5
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Checking a Listbox for duplicate Items

    But that code has to fire every time anything is inputted, why waste the speed?

  6. #6

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Checking a Listbox for duplicate Items

    I just ran a test and in a list with 200 entries removing duplicates after the fact took 200+ milliseconds while preventing them in the first place took only 30+ milliseconds.

    I forgot a couple of declarations in my code above.

    Private Const LB_FINDSTRINGEXACT = &H1A2
    Private Const LB_ADDSTRING = &H180

  8. #8
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Checking a Listbox for duplicate Items

    Your code crashes vb
    VB Code:
    1. Call SendMessage(MyListbox.hwnd, LB_ADDSTRING, 0, ByVal strStringToBeAdded)

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Checking a Listbox for duplicate Items

    Wow, I messed myself up not using a collection.
    This doesn't do anything but display the dups. Not elegant, but works.
    Commented, also

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   List1.AddItem "123"
    5.   List1.AddItem "456"
    6.   List1.AddItem "123"
    7.   List1.AddItem "456"
    8.   FindDups
    9. End Sub
    10.  
    11. Sub FindDups()
    12.   Dim c As Integer, lc As Integer, dp As Integer
    13.   Dim x As Integer, y As Integer, dc As Integer
    14.   Dim temp As String, dups() As String
    15.   Dim str As String, g As Integer
    16.   Dim found As Boolean
    17.   c = List1.ListCount - 1
    18.   ReDim arr(c) As String
    19.   dc = 0
    20.   For x = 0 To c
    21.     arr(x) = List1.List(x) ' Load into array
    22.   Next x
    23.   For x = 0 To c
    24.     temp = List1.List(x) ' use one item at a time
    25.     lc = 0
    26.     For y = 0 To c
    27.       If temp = arr(y) Then ' compare to array to find dups
    28.          lc = lc + 1
    29.       End If
    30.     Next y
    31.     If lc > 1 Then ' if dups
    32.       For dp = 0 To dc
    33.         If dc > 0 Then
    34.           For g = 0 To dc - 1 ' check if already in list
    35.             If dups(g) = List1.List(x) Then
    36.               found = True  ' already in list
    37.               Exit For
    38.             End If
    39.           Next g
    40.         End If
    41.         If found = True Then Exit For
    42.         ReDim Preserve dups(dc) As String
    43.         dups(dc) = List1.List(x) ' add to list
    44.         dc = dc + 1
    45.       Next dp
    46.     End If
    47.   Next x
    48.   '  display list
    49.   str = "  Dups" & vbCrLf
    50.   For x = 0 To dc - 1
    51.     str = str & dups(x) & vbCrLf
    52.   Next x
    53.   MsgBox str
    54. End Sub

  10. #10
    Hyperactive Member
    Join Date
    May 2005
    Posts
    324

    Re: Checking a Listbox for duplicate Items

    To answer the original question, if you sorted the listbox first, you could amend 2eM!x's code to make it faster:
    Quote Originally Posted by |2eM!x
    VB Code:
    1. Private Function ChkLst(iList As ListBox)
    2.     Dim i As Integer, x As Integer
    3.         For i = 0 To iList.ListCount - 1
    4.             [COLOR=Red]For x = i + 1 To iList.ListCount - 1[/COLOR]
    5.                 If (iList.List(i) = iList.List(x)) And x <> i Then
    6.                     List1.RemoveItem (i)
    7.                 [COLOR=Red]else
    8.                     Next i[/COLOR]
    9.                 End If
    10.             Next x
    11.         Next i
    12.         MsgBox iList.ListCount
    13. End Function
    14.  
    15. Private Sub Command1_Click()
    16.     Call ChkLst(List1)
    17. End Sub
    Last edited by anguswalker; Aug 12th, 2005 at 01:07 AM.

  11. #11
    Hyperactive Member
    Join Date
    May 2005
    Posts
    324

    Re: Checking a Listbox for duplicate Items

    Actually I've just realised the code from #3 or #10 would only remove alternate instances of duplicates. If say i was 1 and item 2 was identical, you remove item 2, so item 3 becomes item 2, and never gets checked. Change it to:
    VB Code:
    1. Private Function ChkLst(iList As ListBox)
    2.     Dim i As Integer, x As Integer
    3.         For i = 0 To iList.ListCount - 1
    4.             do while iList.ListCount > i + 2
    5.                 If (iList.List(i) = iList.List(i+1)) Then
    6.                     List1.RemoveItem (i + 1)
    7.                 else
    8.                     Next i
    9.                 End If
    10.             loop
    11.         Next i
    12.         MsgBox iList.ListCount
    13. End Function
    14.  
    15. Private Sub Command1_Click()
    16.     Call ChkLst(List1)
    17. End Sub
    Last edited by anguswalker; Aug 12th, 2005 at 01:08 AM.

  12. #12
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Checking a Listbox for duplicate Items

    Quote Originally Posted by |2eM!x
    Your code crashes vb
    VB Code:
    1. Call SendMessage(MyListbox.hwnd, LB_ADDSTRING, 0, ByVal strStringToBeAdded)
    Not if used correctly. Is the variable that you used for strStringToBeAdded actually a string?

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Checking a Listbox for duplicate Items

    For anyone who is interested I've attached a project that does it both ways. BTW, I modified my code slightly and now it runs in about 16 ms.
    Attached Files Attached Files

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