listbox remove duplicates

RandomCoder

Well-Known Member
Licensed User
Longtime User
Hi mate,

Before adding the value do this...
B4X:
   For i=0 To Lb.Count-1
   If NewValue=Lb.Item(i) Then NewValue = ""
   Next
   If NewValue<>"" Then Lb.Add(NewValue)

Of course if the values originally came from a table or if the list is very long then I would recommend using an SQL select distinct query to populate the listbox with unique values.

Regards,
RandomCoder

PS It was my turn to do the feed... not normally up at this time :(
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
PS It was my turn to do the feed... not normally up at this time
:) Don't worry. You are not alone in these strange hours.

Another good and fast alternative is to also add the items to a Hashtable as keys. The values can be empty strings.
Then you can check if the item exists with Hashtable.contains.
This will be much faster then checking if the item exists in the list.
B4X:
Sub Globals
    'Declare the global variables here.

End Sub

Sub App_Start
    Form1.Show
    'Testing code
    For i = 1 To 100
        AddItemToListBox(Rnd(1,20))
    Next
End Sub

Sub AddItemToListBox (item)
    If Hashtable1.Contains(item) = False Then
        Hashtable1.Add(item, "")
        ListBox1.Add(item)
    End If
End Sub
 
Top