Help making a duplicate check for listbox.

DeltaWolf7

Well-known member
Joined
Jan 21, 2006
Messages
47
Programming Experience
Beginner
I have written this code which does work, meaning no erros, but i dont have the knowlege to work out where it does not remove duplicates from my list.

Please can someone help me out?


VB.NET:
[SIZE=2][COLOR=#008000]' Check for Duplicates
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] itemsArray() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' get number of items in list.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ItemCount [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = lstBox.Items.Count - 1
[/SIZE][SIZE=2][COLOR=#008000]' initilize array with slots for all items in list.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ReDim[/COLOR][/SIZE][SIZE=2] itemsArray(ItemCount)
[/SIZE][SIZE=2][COLOR=#008000]' create a variable to hold the position in the array to place the next item.
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' starts at position 1 because 0 will be filled with data on line after.
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Dim NextIndex As Integer = 1
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' this is the data filled in to the array at pos 0.
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'itemsArray(0) = lstBox.Items.Item(ItemCount).ToString
 
 
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] ItemCount
Debug.WriteLine([/SIZE][SIZE=2][COLOR=#800000]"i: "[/COLOR][/SIZE][SIZE=2] & i)
itemsArray(i) = lstBox.Items.Item(i).ToString
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] item [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] itemsArray
Debug.WriteLine([/SIZE][SIZE=2][COLOR=#800000]"item: "[/COLOR][/SIZE][SIZE=2] & item)
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] itemsArray.IndexOf(itemsArray, item) <> i [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] item = lstBox.Items.Item(ItemCount).ToString [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]Debug.WriteLine([/SIZE][SIZE=2][COLOR=#800000]"lstbox: "[/COLOR][/SIZE][SIZE=2] & lstBox.Items.Item(i))
lstBox.Items.RemoveAt(i)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE]
 
you want to remove the duplicate data in out listbox?
the listbox data is blind from database or is already in the listbox?

lucky your code do not remove your duplicate data in lstBox. Because after your remove the data your code will have problem because system cannot read from the item.

which mean after you run below code to remove duplicate data your:
VB.NET:
lstBox.Items.RemoveAt(i)

error will appear at below line because of system cannot get the item in your lstBox:
VB.NET:
itemsArray(i) = lstBox.Items.Item(i).ToString
 
Last edited by a moderator:
The data is loaded from a text file into the lstbox.

Yes, I want to remove duplicate items from the list. If possible I would like the ability to also save the duplicates found.
 
why you need to remove the duplidate data but you stil need to save in?

i suggest you doing the checking function before blind your data into listbox.

so when every time you bind your data into listbox do checking first check whether the data is in the listbox or not, if not in the listbox then bind the data to listbox otherwise just exit the function do nothing.
 
The data that is loaded into the listbox are web addresses. I want to remove the duplicates while the data is bound to the listbox.
What if i created an array or item collection to hold the data and then ran the function on that before binding it to the listbox? Whould that be more do-able? If so how do I modify my code to work from a non-listbox data source?

The reason I want to save the duplicates is so then the user can refer back to them if needed. The save isnt need and I can get away without it.
The main part i want to get working is the duplicate removal part. The rest can wait.
 
he easiest way to achive this is to check each item in the listbox before adding a new item

but since things are data binded and whatnot you could try something like:
VB.NET:
For Each Str1 As String In lstBox.Items
  For Each Str2 As String In lstBox.Items
    If Str1 = Str2 Then
      'Duplicate was found
    End If
  Next Str2
Next Str1
 
VB.NET:
For i AsInteger = 0 To lstBox.Items.Count - 1
If lstBox.Items(i) = sItem Then'sItem: is item you get from your text file
IsExists = True 'IsExists: is boolean
ExitFor
Else
IsExists = False
EndIf
Next
 
If IsExists = FalseThen
lstBox.Items.Add(sItem)
EndIf
 
Last edited by a moderator:
Thank you everyone.

I managed to work something out which although simple works form me for now.

Thanks again.

BTW here is the code with a little help from your examples.

VB.NET:
[SIZE=2][COLOR=#008000]'Duplicate check.
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] NewList [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ListBox
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Count [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#008000]' duplicate check code with help from http://www.vbdotnetforums.com/showthread.php?t=10321
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] lstBox.Items.Count - 1
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] NewList.FindStringExact(lstBox.Items(i)) <> -1 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]Count += 1
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]NewList.Items.Add(lstBox.Items(i))
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2]lstBox.Items.Clear()
lstBox.Items.AddRange(NewList.Items)
ischanged = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2]FileChanged.Text = [/SIZE][SIZE=2][COLOR=#800000]"File Modified"
[/COLOR][/SIZE][SIZE=2]FileChanged.ForeColor = Color.Red
MsgBox(Count & [/SIZE][SIZE=2][COLOR=#800000]" number of duplicates where removed."[/COLOR][/SIZE][SIZE=2], MsgBoxStyle.Information, [/SIZE][SIZE=2][COLOR=#800000]"Duplicates"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]
[/SIZE]
 
dupe checks are better done with hashtables:

VB.NET:
Dim ht as new Hashtable
Dim line as String
 
While Not inputFile.EOF 'code to read from file etc..
 
  line = inputFile.ReadLine()
 
  if ht.containsKey(line) then
    msgbox "Duplicate line encountered"
  else
    ht.add(line, nothing)
    listBox.addItem(line)
  endif
 
wend
 
Back
Top