Preventing duplicate items in a List/ComboBox

This code demonstrates how to add items to a List/Combo box without duplicating any items.

Just do: Call ListAddItem(List1, "My New Item", 0)

Replace List1 with a List box/Combo box name, then name of the item your adding and whether or not its case sensitive.

Sub ListAddItem(lst As Control, sItem As String, CaseSense As Integer)
'CaseSense = 0 For CaSe SeNsItIvE
'CaseSense = 1 for not case sensitive
' works with List, Combo
For a = 0 To lst.ListCount - 1
    If InStr(1, lst.List(a), sItem, CaseSense) _
And Len(lst.List(a)) = Len(sItem) Then Exit Sub
Next
lst.AddItem sItem
End Sub

You might also like...

Comments

Kym Manson Till the Roof comes off Till the Lights go out Till my Legs give out Can't shut my mouth I will not fall, my Wisdoms all.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.” - Cory Doctorow