Thread Rating:
  • 2 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[How To] Remove Duplicates From A Listbox
#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


Messages In This Thread
RE: [How To] Remove Duplicates From A Listbox - by euverve - 05-12-2011, 10:46 AM

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,925 07-03-2011, 05:33 AM
Last Post: H-Q
  Listbox Help Danny 8 3,531 04-18-2010, 12:37 AM
Last Post: RaZoR03
  Save Listbox Danny 5 2,417 04-18-2010, 12:35 AM
Last Post: RaZoR03

Forum Jump:


Users browsing this thread: 2 Guest(s)