Results 1 to 2 of 2

Thread: VB - Remove duplicates from ListBoxes

  1. #1

    Thread Starter
    Fanatic Member joltremari's Avatar
    Join Date
    Sep 2000
    Location
    Mississippi
    Posts
    674

    VB - Remove duplicates from ListBoxes

    VB Code:
    1. Public Sub RemoveDups(ListName As ListBox)
    2.  
    3. 'REMOVE DUPLICATES
    4.  
    5.     With ListName    
    6.         For i = 0 To .ListCount - 1
    7.             For j = .ListCount To (i + 1) Step -1
    8.            
    9.                 If .List(j) = .List(i) Then                    
    10.                     .RemoveItem j
    11.                 End If
    12.                
    13.             Next
    14.         Next
    15.     End With
    16.  
    17. End Sub
    18.  
    19.  
    20.  
    21. 'USAGE
    22.  
    23. RemoveDups List1
    "I have not failed. I've just found 10,000 ways that won't work."
    'Thomas Edison'

    "If we knew what it was we were doing it wouldn't be called research, would it?"
    'Albert Einstein'

    VB6

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    I prefer the API route, but they both work fine...
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    4.                                                                            ByVal wMsg As Long, _
    5.                                                                            ByVal wParam As Long, _
    6.                                                                            ByVal lParam As String) _
    7.                                                                            As Long
    8.  
    9. Private Const LB_ERR = (-1)
    10. Private Const LB_FINDSTRINGEXACT = &H1A2
    11.  
    12. Private Sub Command1_Click()
    13.     RemoveDuplicates List1
    14. End Sub
    15.  
    16. Private Sub RemoveDuplicates(lst As ListBox)
    17. Dim lPos As Long, x As Long
    18.  
    19.     For x = (lst.ListCount - 1) To 0 Step (-1)
    20.         lPos = SendMessageStr(lst.hwnd, LB_FINDSTRINGEXACT, _
    21.                               LB_ERR, lst.List(x))
    22.        
    23.         While ((lPos <> LB_ERR) And (lPos <> x))
    24.             Call lst.RemoveItem(lPos)
    25.             lPos = SendMessageStr(lst.hwnd, LB_FINDSTRINGEXACT, _
    26.                                   LB_ERR, lst.List(x))
    27.         Wend
    28.     Next
    29.  
    30. End Sub
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

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