Results 1 to 5 of 5

Thread: Duplicate ListBox entry

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Posts
    111

    Duplicate ListBox entry

    How would I prevent a Duplicate ListBox entry?

    So if Apple is entered the user can not repeat and enter Apple again

    It seems I should loop through this but how exactly?

    vbMarkO

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    The loop way:

    VB Code:
    1. For i = 0 To List1.ListCount - 1
    2.     If List1.List(i) = "Apple" Then
    3.         MsgBox "Apple already entered"
    4.     End If
    5. Next i

    You can also use SendMessage, though I forget the actual message (somebody'll most likely post it).
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Posts
    111
    Originally posted by jemidiah
    The loop way:

    VB Code:
    1. For i = 0 To List1.ListCount - 1
    2.     If List1.List(i) = "Apple" Then
    3.         MsgBox "Apple already entered"
    4.     End If
    5. Next i

    You can also use SendMessage, though I forget the actual message (somebody'll most likely post it).
    Thanx

  4. #4
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Originally posted by jemidiah
    You can also use SendMessage, though I forget the actual message (somebody'll most likely post it).
    LB_FINDSTRINGEXACT or LB_FINDSTRING
    Please rate my post.

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Here is a complete example that you can modify for either a listbox or a combobox. Using SendMessage will be much faster that looping through the listbox or combobox.

    VB Code:
    1. Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" _
    2.                       (ByVal hWnd As Long, _
    3.                        ByVal wMsg As Long, _
    4.                        ByVal wParam As Long, _
    5.                        ByVal lParam As String) As Long
    6. Private Const CB_FINDSTRINGEXACT = &H158
    7. Private Const LB_FINDSTRINGEXACT = &H1A2
    8.  
    9.                 Dim lngRetVal As Long
    10.  
    11.                 lngRetVal = SendMessageString(MyListBox.hWnd, _
    12.                                               LB_FINDSTRINGEXACT, -1&, _
    13.                                               "string to find")
    14.                 If lngRetVal > -1& Then
    15.                     ' It's already in the list and lngRetVal is the listindex
    16.                 Else
    17.                     MyListBox.Additem "string to find"
    18.                 End If

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