Support Forums
[How To] Remove Duplicates From A Listbox - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Visual Basic and the .NET Framework (https://www.supportforums.net/forumdisplay.php?fid=19)
+---- Thread: [How To] Remove Duplicates From A Listbox (/showthread.php?tid=18622)



[How To] Remove Duplicates From A Listbox - PURP - 05-08-2011

I do not know if this has been posted before but I thought someone might find this useful.

[Image: s0gyB.gif]

Here is the code



RE: [How To] Remove Duplicates From A Listbox - KoBE - 05-08-2011

Good share PURP, it should help some people out.


RE: [How To] Remove Duplicates From A Listbox - iCrack - 05-09-2011

Nice share PURP! ;)


RE: [How To] Remove Duplicates From A Listbox - PURP - 05-09-2011

(05-08-2011, 03:16 PM)KoBE Wrote: Good share PURP, it should help some people out.

Thank you KoBE Smile

(05-09-2011, 12:57 PM)iCrack Wrote: Nice share PURP! ;)

Why thank you very much!


RE: [How To] Remove Duplicates From A Listbox - euverve - 05-12-2011

Since the code is in image, and cannot be copy and pasted. I post for copy and paste purposes. A bit slight change in codings but same output as the TS.

Full codes:
Code:
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'This opens a .txt to the listbox
        Try
            Using asd As New OpenFileDialog() With {.Filter = ("Text Files|*.txt")}
                asd.ShowDialog()
                Using streamread As New IO.StreamReader(asd.FileName)
                    While (streamread.Peek() > -1)
                        ListBox1.Items.Add(streamread.ReadLine)
                    End While
                End Using
            End Using
        Catch ex As Exception
        End Try
        Label1.Text = ListBox1.Items.Count
    End Sub


    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        'This removes all duplicates from a listbox
        Dim i, j As Integer
        Dim arr As New ArrayList
        Dim itemfound As Boolean
        For i = 0 To ListBox1.Items.Count - 1
            itemfound = False
            For j = 0 To i - 1
                If ListBox1.Items.Item(i) = ListBox1.Items.Item(j) Then
                    itemfound = True
                    Exit For
                End If
            Next j

            If Not itemfound Then
                arr.Add(ListBox1.Items.Item(i))
            End If
        Next i
        ListBox1.Items.Clear()
        ListBox1.Items.AddRange(arr.ToArray)
        arr = Nothing
        Label1.Text = ListBox1.Items.Count
    End Sub
End Class



RE: [How To] Remove Duplicates From A Listbox - iCrack - 05-12-2011

(05-12-2011, 10:46 AM)euverve Wrote: Since the code is in image, and cannot be copy and pasted. I post for copy and paste purposes. A bit slight change in codings but same output as the TS.

Full codes:
Code:
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'This opens a .txt to the listbox
        Try
            Using asd As New OpenFileDialog() With {.Filter = ("Text Files|*.txt")}
                asd.ShowDialog()
                Using streamread As New IO.StreamReader(asd.FileName)
                    While (streamread.Peek() > -1)
                        ListBox1.Items.Add(streamread.ReadLine)
                    End While
                End Using
            End Using
        Catch ex As Exception
        End Try
        Label1.Text = ListBox1.Items.Count
    End Sub


    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        'This removes all duplicates from a listbox
        Dim i, j As Integer
        Dim arr As New ArrayList
        Dim itemfound As Boolean
        For i = 0 To ListBox1.Items.Count - 1
            itemfound = False
            For j = 0 To i - 1
                If ListBox1.Items.Item(i) = ListBox1.Items.Item(j) Then
                    itemfound = True
                    Exit For
                End If
            Next j

            If Not itemfound Then
                arr.Add(ListBox1.Items.Item(i))
            End If
        Next i
        ListBox1.Items.Clear()
        ListBox1.Items.AddRange(arr.ToArray)
        arr = Nothing
        Label1.Text = ListBox1.Items.Count
    End Sub
End Class

He probably didn't want people to copy & paste... Thumbsup


RE: [How To] Remove Duplicates From A Listbox - PURP - 05-12-2011

(05-12-2011, 11:30 AM)iCrack Wrote: He probably didn't want people to copy & paste... Thumbsup

I didn't :/ I don't like when people c&p..

Atleast when people type it out it helps then understand some.