Adding Items to a Listbox

Hello. I need to said to a Listbox that, if the item have already add it, when i try to add it again don’t add it. Thanks in advance

difficult to understand the question

you mean Duplicates?

You must search the listbox for a duplicate and add or not based on the results. The listbox itself has nothing built in to help you with this.

  Dim source As Listbox = lb // your ListBox Name
  Dim i, j As Integer
  For i = 0 To source.ListCount-1
    For j = i + 1 To source.ListCount-1
      If source.Cell(i, -1) = source.Cell(j, -1) Then
        source.RemoveRow(j)
      End If
    Next
  Next

[quote=182217:@Axel Schneider]difficult to understand the question

you mean Duplicates?[/quote]I mean that don´t duplicate, that for example, i add a item called “Example 1”, i add other item called “Example 2”, and then i add other called “Example 1” but there are 2 “Example 1”, that is what i like to fix, that don´t duplicate the items. Do you understand?

@Axel: He wants to add each item only once.

Assuming one column:

  Dim source As Listbox = lb // your ListBox Name
  Dim s as string = "your new value"
  Dim i As Integer
  Dim AlreadyThere as Boolean = False

  For i = 0 To source.ListCount-1
      If source.Cell(i, 0) = s Then 
            AlreadyThere = True
            Exit For i // or Return if inside method
      End if
  Next

  If Not AlreadyThere then source.addRow s 

Or because I love using extends:

[code]Sub add_unique_column(extends lb as Listbox, column_to_search as integer, text_to_add as string)
dim i as integer
dim found as Boolean = false

for i = 0 to lb.ListCount - 1
if lb.cell(i, column_to_search) = text_to_add then
found = true
exit for i
end if
next

if not found then
lb.AddRow("")
lb.Cell(lb.LastIndex, column_to_search) = text_to_add
end if

End Sub[/code]

Thanks too all!