Removing duplicates in listbox

  • Thread starter Thread starter kimiraikkonen
  • Start date Start date
K

kimiraikkonen

Hi,
Assume you have the same item more than one quantity in listbox. Eg:
1
1
2
2
3
3

How can i delete the duplicated entries and it should remain as:

1
2
3

Thanks!
 
Hi,

I had the same problem when trying to add file names to a listbox. The solution is as follows using my example:

'Catch duplicate files being added
Try

colFiles.Add(FileName, FileName) 'File names are only added once

Catch ex As Exception

Exit Try

End Try

The Add method throws an exception if the same file name is added.

Hope this helps.

Robert
 
kimiraikkonen said:
Hi,
Assume you have the same item more than one quantity in listbox. Eg:
1
1
2
2
3
3

How can i delete the duplicated entries and it should remain as:

1
2
3

Thanks!

I'd refill the listbox without the duplicates.

Remove duplicates from Array (VB 2008):

a = a.Distinct.ToArray 'a is an Array


Armin
 
I'd refill the listbox without the duplicates.

Remove duplicates from Array (VB 2008):

a = a.Distinct.ToArray 'a is an Array

Armin

Sorry i only have VB 2005 and i need to remove duplicates later after
listbox is filled.
 
kimiraikkonen said:
Hi,
Assume you have the same item more than one quantity in listbox. Eg:
How can i delete the duplicated entries and it should remain as:

1. Create a new List (Of String).
2. Loop trough the items in the listbox

While looping:
1. Check if the the item exist in the list
a) if not, add it to the list
b) if exists, remove the item from the listbox

-Teemu
 
1. Create a new List (Of String).
2. Loop trough the items in the listbox

While looping:
1. Check if the the item exist in the list
a) if not, add it to the list
b) if exists, remove the item from the listbox

-Teemu

Could you sample codewith list(of string)?

Remember i need to remove "each" one of duplicated items if there are
any duplicates after the listbox is filled.
 
"kimiraikkonen" <[email protected]> kirjoitti viestissä
"kimiraikkonen" <[email protected]> kirjoitti
viestissä
Could you sample codewith list(of string)?

Remember i need to remove "each" one of duplicated items if there are
any duplicates after the listbox is filled.

ListBox1.Items.Add("a")
ListBox1.Items.Add("b")
ListBox1.Items.Add("c")
ListBox1.Items.Add("b")
ListBox1.Items.Add("c")
ListBox1.Items.Add("a")

Dim newList As New List(Of String)
For i As Integer = 0 To ListBox1.Items.Count - 1
If Not newList.Contains(ListBox1.Items(i).ToString) Then
newList.Add(ListBox1.Items(i).ToString)
End If
Next

For Each element As String In newList
ListBox1.Items.Remove(element)
Next

-Teemu
 
Sorry, my mistake:

ListBox1.Items.Add("a")
ListBox1.Items.Add("b")
ListBox1.Items.Add("c")
ListBox1.Items.Add("b")
ListBox1.Items.Add("c")
ListBox1.Items.Add("a")
ListBox1.Items.Add("1")

Dim newList As New List(Of String)
Dim itemsToRemove As New List(Of String)
For i As Integer = 0 To ListBox1.Items.Count - 1

If newList.Contains(ListBox1.Items(i).ToString) Then
itemsToRemove.Add(ListBox1.Items(i).ToString)
Else
newList.Add(ListBox1.Items(i).ToString)
End If

Next

For Each element As String In itemsToRemove
ListBox1.Items.Remove(element)
Next


-Teemu
 
Sorry, my mistake:

ListBox1.Items.Add("a")
ListBox1.Items.Add("b")
ListBox1.Items.Add("c")
ListBox1.Items.Add("b")
ListBox1.Items.Add("c")
ListBox1.Items.Add("a")
ListBox1.Items.Add("1")

Dim newList As New List(Of String)
Dim itemsToRemove As New List(Of String)
For i As Integer = 0 To ListBox1.Items.Count - 1

If newList.Contains(ListBox1.Items(i).ToString) Then
itemsToRemove.Add(ListBox1.Items(i).ToString)
Else
newList.Add(ListBox1.Items(i).ToString)
End If

Next

For Each element As String In itemsToRemove
ListBox1.Items.Remove(element)
Next

-Teemu

Great, thanks Teemu.
 
Back
Top