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.)
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