Results 1 to 6 of 6

Thread: how can I see if an item exists in the listbox

  1. #1

    Thread Starter
    Addicted Member kill_bill_gates's Avatar
    Join Date
    Oct 2004
    Posts
    222

    Resolved how can I see if an item exists in the listbox

    How can I check if the listbox has the specified item?
    Last edited by kill_bill_gates; Aug 26th, 2005 at 11:59 AM.
    "Quis custodiet ipsos custodes?"
    Juvenal
    Mete the Hun wanted to live in peace with the Chinese. So he gave the Chinese Emperor his favorite horse, best swords in his armory, and lots of other cool stuff. But then the Chinese Emperor asked for one thing. A useless land through the north. It was a small, useless, unproductive, uninhabited piece of land. But Mete the Hun's answer was certain:
    I gave you horses, weapons and much more which belonged to me. But the lands are not mine, it's my nation's and I'm ready to fight, kill and die for just an inch my country
    -=Joey Jordison R0CKS!! =-

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: how can I see if an item exists in the listbox

    VB Code:
    1. Dim i As Long
    2.     With List1
    3.         For i = 0 To .ListCount - 1
    4.             If Text1.Text = .List(i) Then
    5.                MsgBox "Item already exists in List"
    6.                'if it is already there, boogie
    7.                Exit Sub
    8.             End If
    9.         Next
    10.         'if it is not present, add it
    11.         .AddItem Text1.Text
    12.     End With

  3. #3
    Member
    Join Date
    Aug 2005
    Posts
    35

    Re: how can I see if an item exists in the listbox

    Why not Dim i as integer? Are you sure?

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: how can I see if an item exists in the listbox

    Quote Originally Posted by vb6_
    Why not Dim i as integer? Are you sure?
    I'm positive. I always use longs (well, almost always.)

  5. #5

    Thread Starter
    Addicted Member kill_bill_gates's Avatar
    Join Date
    Oct 2004
    Posts
    222

    Re: how can I see if an item exists in the listbox

    thanks
    "Quis custodiet ipsos custodes?"
    Juvenal
    Mete the Hun wanted to live in peace with the Chinese. So he gave the Chinese Emperor his favorite horse, best swords in his armory, and lots of other cool stuff. But then the Chinese Emperor asked for one thing. A useless land through the north. It was a small, useless, unproductive, uninhabited piece of land. But Mete the Hun's answer was certain:
    I gave you horses, weapons and much more which belonged to me. But the lands are not mine, it's my nation's and I'm ready to fight, kill and die for just an inch my country
    -=Joey Jordison R0CKS!! =-

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: how can I see if an item exists in the listbox

    A listbox is limited to 32K items, so an Integer is sufficient. You can also use the SendMessage API to search without looping. Here is an example that I have to search a listbox and a combobox. If it beeps, the item is found, if not, it is added.


    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" _
    4. (ByVal hWnd As Long, _
    5. ByVal wMsg As Long, _
    6. ByVal wParam As Long, _
    7. ByVal lParam As String) As Long
    8. Private Const CB_FINDSTRINGEXACT = &H158
    9. Private Const LB_FINDSTRINGEXACT = &H1A2
    10. Public newitem As String
    11.  
    12.  
    13. Sub searchcombo()
    14. Dim lngRetVal As Long
    15. lngRetVal = SendMessageString(Combo1.hWnd, _
    16. CB_FINDSTRINGEXACT, -1&, _
    17. newitem)
    18. If lngRetVal <> -1& Then
    19.   Beep
    20. ' It's already in the list and lngRetVal is the listindex
    21. Else
    22.   Combo1.AddItem newitem
    23. End If
    24. End Sub
    25.  
    26. Private Sub Form_Load()
    27.   Dim x As Integer
    28.   Combo1.AddItem "test"
    29.   Combo1.AddItem "dog"
    30.   Combo1.AddItem "cat"
    31.   newitem = "frog"
    32.   For x = 0 To 5
    33.     List1.AddItem Chr$(34) & x & Chr$(34)
    34.     List1.ItemData(x) = 0
    35.   Next x
    36.   searchcombo
    37.   newitem = "7"
    38.   searchlist
    39. End Sub
    40.  
    41. Sub searchlist()
    42. Dim lngRetVal As Long
    43. lngRetVal = SendMessageString(List1.hWnd, _
    44. LB_FINDSTRINGEXACT, lngRetVal&, _
    45. newitem)
    46. If lngRetVal <> -1& Then
    47.   Beep
    48. ' It's already in the list and lngRetVal is the listindex
    49. Else
    50.   List1.AddItem Chr$(34) & newitem & Chr$(34)
    51. End If
    52. End Sub

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