Results 1 to 8 of 8

Thread: How do you remove duplicates from a list box?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    91

    How do you remove duplicates from a list box?

    i need to get rid of duplicate entries in a listbox? how would i go about this?

    thanks alot

  2. #2
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: How do you remove duplicates from a list box?

    VB Code:
    1. For i=List1.listcount-1 to 1 step -1
    2.       IF List1.List(i)=List1.List(i-1) Then
    3.           List1.RemoveItem i
    4. Next

  3. #3
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: How do you remove duplicates from a list box?

    That won't check to see if there are duplicates later on in the code. To do that, you'd need two loops, as such. Forgive me if its' buggy, I don't have VB open. I'm running off of zynder's code:
    VB Code:
    1. For i = List1.ListCount - 1 to 1 Step -1
    2.    [b]For j = i To 1 Step -1[/b]
    3.       If List1.List(i) = List1.List(j) Then List1.RemoveItem i
    4.    [b]Next j[/b]
    5. Next i

    As I said, probably a little buggy, but it's the idea that you need.

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: How do you remove duplicates from a list box?

    You could transfer the listbox strings to a collection, set the item and key arguments on collection.Add equal to the string in the listbox... since keys have to be unique in a collection adding repeating keys generates an error... handle the error and continue adding to the collection.

    After transferring to the collection, clear the listbox and move the strings back to the listbox from the collection.

    The iterations are one pass through the listbox items, plus one pass through the collection object.

    EDIT: Here try this
    VB Code:
    1. Private Sub Form_Load()
    2. Dim mycol As New Collection
    3. Dim cnt As Long
    4.  
    5. On Error Resume Next
    6.    For cnt = 0 To List1.ListCount - 1
    7.       mycol.Add List1.List(cnt), List1.List(cnt)
    8.    Next
    9.    If Err Then Err.Clear
    10. On Error GoTo 0
    11.  
    12.    List1.Clear
    13.    For cnt = 1 To mycol.Count
    14.       List1.AddItem mycol(1)
    15.    Next
    16. End Sub
    Last edited by leinad31; Nov 24th, 2006 at 02:14 AM.

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: How do you remove duplicates from a list box?

    use API, something like this (untested):
    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    2.     ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    3.  
    4. Private Const LB_FINDSTRINGEXACT = &H1A2
    5.  
    6. Private Sub Command1_Click()
    7.     Dim lPos As Long, N As Long
    8.  
    9.     Do While N < List1.Count
    10.         Do
    11.             lPos = SendMessage(List1.hWnd, LB_FINDSTRINGEXACT, N, ByVal List1.List(N))
    12.             If lPos > -1 Then List1.RemoveItem lPos
    13.         Loop While lPos > -1
    14.         N = N + 1
    15.     Loop
    16. End Sub

  6. #6
    Member
    Join Date
    Nov 2006
    Posts
    43

    Re: How do you remove duplicates from a list box?

    Quote Originally Posted by bushmobile
    use API, something like this (untested):
    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    2.     ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    3.  
    4. Private Const LB_FINDSTRINGEXACT = &H1A2
    5.  
    6. Private Sub Command1_Click()
    7.     Dim lPos As Long, N As Long
    8.  
    9.     Do While N < List1.Count
    10.         Do
    11.             lPos = SendMessage(List1.hWnd, LB_FINDSTRINGEXACT, N, ByVal List1.List(N))
    12.             If lPos > -1 Then List1.RemoveItem lPos
    13.         Loop While lPos > -1
    14.         N = N + 1
    15.     Loop
    16. End Sub
    You should always delete in a reversed order, loop from List1.Listcont-1 to 0 otherwise you will skip every item following a deleted item.

    EDIT: My mistake, i didn't read the code carefully enough.

  7. #7
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: How do you remove duplicates from a list box?

    This question has been asked many times before, do a search.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  8. #8
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: How do you remove duplicates from a list box?

    @timeshifter: althow that method will remove all duplicates it's very very slow...

    I would suggest to use the API bushmobile provided...

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