|
-
Oct 21st, 2004, 07:19 AM
#1
Thread Starter
Frenzied Member
Removing duplicates ?
ok, now you here, How would i go about removing duplicates from a Listbox ?
i want to do this when a button is clicked, this is basically because when i parse a particular page the first result duplicates itself, so i want to remove any duplicates...
Any help is appreciated !
-
Oct 21st, 2004, 07:29 AM
#2
Addicted Member
The following code will remove duplicates in List1.
It may be a bit slow if listcount is high...
VB Code:
Private colList As Collection
Private Sub cmdRemDuplicate_Click()
Dim nC As Integer
Set colList = New Collection
For nC = 0 To List1.ListCount - 1
Call AddToCollection(List1.List(nC))
Next nC
List1.Clear
For nC = 1 To colList.Count
Call List1.AddItem(colList(nC))
Next nC
End Sub
Private Sub Form_Load()
List1.AddItem "Test1"
List1.AddItem "Test1"
List1.AddItem "An error also occurs if a specified key duplicates the key for an existing member of the collection."
List1.AddItem "An error also occurs if a specified key duplicates the key for an existing member of the collection."
List1.AddItem "Test3"
List1.AddItem "Test4"
List1.AddItem "Test4"
End Sub
Private Sub AddToCollection(ByVal sText As String)
On Error GoTo Duplicate
Call colList.Add(sText, sText)
Exit Sub
Duplicate:
'Item already exists, won't be added to collection
End Sub
-
Oct 21st, 2004, 07:32 AM
#3
Addicted Member
hmm.. faster / better
VB Code:
Private colList As Collection
Private Sub cmdRemDuplicate_Click()
Dim nC As Integer
Set colList = New Collection
While nC < List1.ListCount
If IsDuplicate(List1.List(nC)) Then
Call List1.RemoveItem(nC)
Else
nC = nC + 1
End If
Wend
End Sub
Private Sub Form_Load()
List1.AddItem "Test1"
List1.AddItem "Test1"
List1.AddItem "An error also occurs if a specified key duplicates the key for an existing member of the collection."
List1.AddItem "An error also occurs if a specified key duplicates the key for an existing member of the collection."
List1.AddItem "Test3"
List1.AddItem "Test4"
List1.AddItem "Test4"
End Sub
Private Function IsDuplicate(ByVal sText As String) As Boolean
On Error GoTo Duplicate
Call colList.Add(sText, sText)
IsDuplicate = False
Exit Function
Duplicate:
IsDuplicate = True
End Function
-
Oct 21st, 2004, 07:38 AM
#4
Retired VBF Adm1nistrator
Using a dictionary is the fastest method in VB.
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Oct 21st, 2004, 07:42 AM
#5
Retired VBF Adm1nistrator
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|