eliminate duplicate entries from a list box

rockwell

Active member
Joined
Dec 6, 2005
Messages
36
Programming Experience
Beginner
hi all i am new to vb.net and i dont know how to eliminate duplicate entries in a list box. Could anyone please help me out in this.

Thanks in advance
 
no i am not populating it from a database im actually retrieving it from a file which might contain duplicate entries.

Thanks for your reply .....
 
before you add each item, you should first itterate through the current items and see if it's already listed:
VB.NET:
Private Function CheckForDups(ByVal Text As String) As Boolean
  For Each Item As String In Listbox1.Items
    If Item.ToLower = Text.ToLower Then Return False
  Next Item
  Return True
End Function

'Usage:
If CheckForDups(TextFromFile) = False Then Listbox1.Items.Add(TextFromFile)
'where TextFromFile is the variable of the item from the text file
as an example
 
Thanks a lot for your reply, your code worked very well just that in the if condition i replaced with a true.

Thanks again
 
I prefer this way because of simplicity:


VB.NET:
[SIZE=2][COLOR=#0000ff]
If [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] listbox1.Items.Contains("sometext") [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]     Me[/COLOR][/SIZE][SIZE=2].listbox1.Items.Add("sometext")
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]


Regards ;)
 
Back
Top