Thread Rating:
  • 2 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[How To] Remove Duplicates From A Listbox
#1
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
Reply
#2
Good share PURP, it should help some people out.
Reply
#3
Nice share PURP! ;)
[Image: just-cause-2-header.jpg]
Reply
#4
(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!
Reply
#5
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
USB Anti-virus? Try USB Drive Defender
[Image: 8bQCusS.jpg]
Reply
#6
(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
[Image: just-cause-2-header.jpg]
Reply
#7
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TextBox2.Text | Transfer Text to ListBox Die 3 2,673 01-02-2012, 06:09 PM
Last Post: AceInfinity
  [VB.NET] - ListBox Drag & Drop - [TUT] Fragma 10 7,732 08-25-2011, 07:02 PM
Last Post: AceInfinity
  Need some help - ListBox Counter Fragma 14 4,922 07-03-2011, 05:33 AM
Last Post: H-Q
  Listbox Help Danny 8 3,524 04-18-2010, 12:37 AM
Last Post: RaZoR03
  Save Listbox Danny 5 2,415 04-18-2010, 12:35 AM
Last Post: RaZoR03

Forum Jump:


Users browsing this thread: 4 Guest(s)