Results 1 to 4 of 4

Thread: Checking if an Item in a Listbox Exists

  1. #1

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720

    Checking if an Item in a Listbox Exists

    How would I check if something is already in a ListBox?
    asdf

  2. #2
    Frenzied Member mxnmx's Avatar
    Join Date
    Dec 2001
    Location
    I'm back...now!!!
    Posts
    1,396
    VB Code:
    1. Private Sub cmdAdd_Click()
    2. For X = 0 To List1.ListCount - 1
    3.     If UCase(Trim(List1.List(X))) = UCase(Trim(Text1.Text)) Then
    4.                  MsgBox "Item Already Exists"
    5.                  Exit Sub
    6.     End If
    7. Next
    8. List1.AddItem Text1.Text
    9. End Sub
    Can't Remember Birthdays or Important Dates- Never Miss any Important Date(s)

  3. #3
    Frenzied Member mxnmx's Avatar
    Join Date
    Dec 2001
    Location
    I'm back...now!!!
    Posts
    1,396
    This is the fastest way to prevent duplicates being inserted into a listbox...
    VB Code:
    1. Public Const LB_FINDSTRING = &H18F
    2. Public Const LB_ADDSTRING = &H180
    3. Public Const CB_FINDSTRING = &H14C ' For a combobox
    4. Public Const CB_ADDSTRING = &H143
    5.  
    6. Public Declare Function SendMessageString Lib "user32" Alias "SendMessageA" _
    7.                       (ByVal hwnd As Long, _
    8.                        ByVal wMsg As Long, _
    9.                        ByVal wParam As Long, _
    10.                        ByVal lParam As String) As Long
    11. Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    12.                        (ByVal hwnd As Long, _
    13.                         ByVal wMsg As Long, _
    14.                         ByVal wParam As Long, _
    15.                         lParam As Any) As Long
    16.  
    17.  
    18. Dim lngRetVal As Long
    19.  
    20.         lngRetVal = SendMessageString(MyListbox.hwnd, _
    21.                                       LB_FINDSTRING, -1&, _
    22.                                       strStringToBeAdded)
    23.         ' lngRetVal is the ListIndex
    24.         If lngRetVal > -1& Then
    25.             ' It's in the list
    26.         Else
    27.             ' It's not in the list so add it
    28.         Call SendMessage(MyListbox.hwnd, LB_ADDSTRING, 0, ByVal strStringToBeAdded)
    29.         End If
    Can't Remember Birthdays or Important Dates- Never Miss any Important Date(s)

  4. #4

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720
    Thanks, I had the same code eccept I was trying to use list1.index with Ubound (lol) instead of Listcount.
    asdf

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width