Remove Duplicate Items

Discussion in 'Visual Basic .NET' started by astradamasta, Feb 17, 2009.

  1. astradamasta

    astradamasta Registered Member

    Joined:
    Nov 24, 2008
    Messages:
    56
    Likes Received:
    8
    Hey i am trying to remove duplicate items from 2 listboxes.... i have the code to remove duplicate items from one list box...i'm trying to build a database of names and i dont want the names to be on there twice. So I'm wanting one list to be the database or names and the other list to be new names.. Lets say i have the database list that has 1,000 names and the other list has 200 names...I want to have a button that when it is clicked it will remove any names from the second list if it is already in the database list....here is the code i have to remove duplicates

    Public Sub RemoveDuplicateItem(ByVal listboxName As ListBox)
    listboxName.Sorted = True
    listboxName.Refresh()
    Dim index As Integer
    Dim itemcount As Integer = listboxName.Items.Count

    If itemcount > 1 Then
    Dim lastitem As String = listboxName.Items(itemcount - 1)

    For index = itemcount - 2 To 0 Step -1
    If listboxName.Items(index) = lastitem Then
    listboxName.Items.RemoveAt(index)
    Else
    lastitem = listboxName.Items(index)
    End If
    Next
    End If
    End Sub

    Any suggestions or a better way to do this?
     
  2. Skank

    Skank Newbie

    Joined:
    Nov 3, 2009
    Messages:
    47
    Likes Received:
    8
    Get rid of that removeduplicates sub. All you need to do to remove duplicates is to add a couple lines to the code that you're using to load the list.

    For example:

    if listbox1.items.contains(strItem) = false then
    listbox1.items.add(strItem)
    end if

    It's basically saying if the list doesn't already contain strItem then add it.
     
  3. Skank

    Skank Newbie

    Joined:
    Nov 3, 2009
    Messages:
    47
    Likes Received:
    8
    Post the code you're using to load the list in the first place and I'll modify it to work as you need it to.