VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Fast way to remove all duplicates (dupes) in a ListBox

by Fredrik Schultz (2 Submissions)
Category: Miscellaneous
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (5 Votes)

This method removes all duplicates in a listbox, regardless if sorting is turned on or not.
AND it's fast, short and simple
(no double loops like in some other submissions).
It's also case-insensitive.

Inputs
Call by using: RemoveDupes MyListBox (where MyListBox is the listbox that will be cleaned of all duplicates.)
Assumes
Call by using: RemoveDupes MyListBox (where MyListBox is the listbox that will be cleaned of all duplicates.)

Rate Fast way to remove all duplicates (dupes) in a ListBox

Private Sub RemoveDupes(lst As ListBox)
 Dim iPos As Integer
 iPos=0
 '-- if listbox empty then exit..
 If lst.ListCount < 1 Then Exit Sub
 Do While iPos < lst.ListCount
  lst.Text = lst.List(iPos)
  '-- check if text already exists..
  If lst.ListIndex <> iPos Then
   '-- if so, remove it and keep iPos..
   lst.RemoveItem iPos
  Else
   '-- if not, increase iPos..
   iPos = iPos + 1
  End If
 Loop
 '-- used to unselect the last selected line..
 lst.Text = "~~~^^~~~"
End Sub

Download this snippet    Add to My Saved Code

Fast way to remove all duplicates (dupes) in a ListBox Comments

No comments have been posted about Fast way to remove all duplicates (dupes) in a ListBox. Why not be the first to post a comment about Fast way to remove all duplicates (dupes) in a ListBox.

Post your comment

Subject:
Message:
0/1000 characters