Advertisement
ASP_Volume2 Miscellaneous #31243

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

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.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
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
Original Comments (3)
Recovered from Wayback Machine