Author Topic: Prevent duplicate entries in listbox  (Read 5188 times)

0 Members and 1 Guest are viewing this topic.

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Prevent duplicate entries in listbox
« on: November 07, 2008, 02:17:55 PM »
Anyone have any code that would prevent a duplicate entry from being added to a listbox?  VBA
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Prevent duplicate entries in listbox
« Reply #1 on: November 07, 2008, 02:55:10 PM »
Just came across this..


Code: [Select]
Public Function NoDuplicatesInList(lst As ListBox, str As String)
    Dim tf As Boolean
    Dim x As Integer
   
    If lst.ListCount = 0 And str <> "" Then
        lst.AddItem str
        'check to make sure no duplicate entries get into list box
    Else
        tf = False
        For x = 0 To lst.ListCount - 1
            If lst.List(x) <> str Then
                tf = False
            Else
                tf = True
                Exit For
            End If
        Next x
        If tf = False And str <> "" Then
            lst.AddItem str
        End If
    End If
End Function
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

rogue

  • Guest
Re: Prevent duplicate entries in listbox
« Reply #2 on: November 10, 2008, 01:21:56 PM »
Anyone have any code that would prevent a duplicate entry from being added to a listbox?  VBA

If Its a listbox, then items can only be added thru code, by you. I would keep a separate string array of the items that are in the listbox, and zip thru the items in the array - accessing properties of controls can get very slow, especially if you have a lot of them ....