# Adding Items to a Listbox

**URL:** <https://forum.xojo.com/t/adding-items-to-a-listbox/23793>\
**Category:** Getting Started\
**Created:** [April 23, 2015, 8:30pm UTC](https://forum.xojo.com/t/adding-items-to-a-listbox/23793 "2015-04-23T20:30:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Federico\_Jos\_D\_Andre](https://forum.xojo.com/letter_avatar_proxy/v4/letter/f/b782af/32.png) [@Federico\_Jos\_D\_Andre](https://forum.xojo.com/u/Federico_Jos_D_Andre)\
**Post date:** [April 23, 2015, 8:30pm UTC](https://forum.xojo.com/t/adding-items-to-a-listbox/23793/1 "2015-04-23T20:30:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Axel\_Schneider](https://forum.xojo.com/user_avatar/forum.xojo.com/axel_schneider/32/465_2.png) [@Axel\_Schneider](https://forum.xojo.com/u/Axel_Schneider)\
**Post date:** [April 23, 2015, 8:39pm UTC](https://forum.xojo.com/t/adding-items-to-a-listbox/23793/2 "2015-04-23T20:39:05Z")

</div>

difficult to understand the question

you mean Duplicates?

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [April 23, 2015, 8:49pm UTC](https://forum.xojo.com/t/adding-items-to-a-listbox/23793/3 "2015-04-23T20:49:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Axel\_Schneider](https://forum.xojo.com/user_avatar/forum.xojo.com/axel_schneider/32/465_2.png) [@Axel\_Schneider](https://forum.xojo.com/u/Axel_Schneider)\
**Post date:** [April 23, 2015, 8:57pm UTC](https://forum.xojo.com/t/adding-items-to-a-listbox/23793/4 "2015-04-23T20:57:53Z")

</div>

```auto
  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
```

---

<div class="post-metadata">

**Author:** ![Federico\_Jos\_D\_Andre](https://forum.xojo.com/letter_avatar_proxy/v4/letter/f/b782af/32.png) [@Federico\_Jos\_D\_Andre](https://forum.xojo.com/u/Federico_Jos_D_Andre)\
**Post date:** [April 23, 2015, 9:11pm UTC](https://forum.xojo.com/t/adding-items-to-a-listbox/23793/5 "2015-04-23T21:11:50Z")

</div>

[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?

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [April 23, 2015, 9:14pm UTC](https://forum.xojo.com/t/adding-items-to-a-listbox/23793/6 "2015-04-23T21:14:30Z")

</div>

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

Assuming one column:

```auto
  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 
```

---

<div class="post-metadata">

**Author:** ![Scott\_Griffitts](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@Scott\_Griffitts](https://forum.xojo.com/u/Scott_Griffitts)\
**Post date:** [April 23, 2015, 9:26pm UTC](https://forum.xojo.com/t/adding-items-to-a-listbox/23793/7 "2015-04-23T21:26:28Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Federico\_Jos\_D\_Andre](https://forum.xojo.com/letter_avatar_proxy/v4/letter/f/b782af/32.png) [@Federico\_Jos\_D\_Andre](https://forum.xojo.com/u/Federico_Jos_D_Andre)\
**Post date:** [April 24, 2015, 12:14am UTC](https://forum.xojo.com/t/adding-items-to-a-listbox/23793/8 "2015-04-24T00:14:34Z")

</div>

Thanks too all!

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:43pm UTC](https://forum.xojo.com/t/adding-items-to-a-listbox/23793/9 "2020-10-29T17:43:57Z")

</div>


