VB - Search in combobox or listbox for a string-VBForums
Results 1 to 3 of 3

Thread: VB - Search in combobox or listbox for a string

  1. #1

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Other side of town from si_the_geek
    Posts
    7,171

    VB - Search in combobox or listbox for a string

    Add a listbox (List1) and a combobox (Combo1), a textbox (Text1) and two command buttons (Command1 , Command2) to your form and add the following code :

    VB Code:
    1. Option Explicit
    2. Private Const CB_FINDSTRING = &H14C
    3. Private Const LB_FINDSTRING = &H18F
    4. Private Declare Function SendMessage Lib _
    5.     "user32" Alias "SendMessageA" (ByVal _
    6.     hwnd As Long, ByVal wMsg As Long, _
    7.     ByVal wParam As Long, lParam As Any) _
    8.     As Long
    9.  
    10. Private Sub Command1_Click()
    11.     FindCB Combo1, Text1.Text
    12. End Sub
    13.  
    14. Private Sub Command2_Click()
    15.     FindLB List1, Text1.Text
    16. End Sub
    17.  
    18. Private Sub Form_Load()
    19.     Command1.Caption = "Find in Combo Box"
    20.     Command2.Caption = "Find in List Box"
    21.  
    22.     Combo1.AddItem "one"
    23.     Combo1.AddItem "two"
    24.     Combo1.AddItem "three"
    25.     Combo1.AddItem "four"
    26.     Combo1.AddItem "five"
    27.     Combo1.AddItem "six"
    28.     Combo1.AddItem "seven"
    29.  
    30.     List1.AddItem "one"
    31.     List1.AddItem "two"
    32.     List1.AddItem "three"
    33.     List1.AddItem "four"
    34.     List1.AddItem "five"
    35.     List1.AddItem "six"
    36.     List1.AddItem "seven"
    37. End Sub
    38.  
    39. Private Sub FindLB(obj As Object, TextToFind As String)
    40.     obj.ListIndex = SendMessage( _
    41.        obj.hwnd, LB_FINDSTRING, -1, ByVal _
    42.        TextToFind)
    43. End Sub
    44.  
    45. Private Sub FindCB(obj As Object, TextToFind As String)
    46.     obj.ListIndex = SendMessage( _
    47.        obj.hwnd, CB_FINDSTRING, -1, ByVal _
    48.        TextToFind)
    49. End Sub
    Last edited by manavo11; Mar 13th, 2003 at 06:32 AM.


    Has someone helped you? Then you can Rate their helpful post.

  2. #2
    Hyperactive Member eranfox's Avatar
    Join Date
    May 2001
    Posts
    492

    Re: VB - Search in combobox or listbox for a string

    Hello manavo11,
    Why do you have to use API's?

    cant you just search it like this?

    VB Code:
    1. Public Function CheckIfExistInCombo(objCombo As Object,TextToFind As String) As Boolean
    2.     Dim NumOfItems As Variant 'The Number Of Items In ComboBox
    3.     Dim IndexNum As Integer 'Index
    4.    
    5.     NumOfItems = objCombo.ListCount
    6.     For IndexNum = 0 To NumOfItems - 1
    7.         If objCombo.List(IndexNum) = TextToFind.Text Then
    8.             CheckIfExistInCombo = True
    9.             Exit Function
    10.         End If
    11.     Next IndexNum
    12.    
    13.     CheckIfExistInCombo = False
    14. End Function

    is API faster than simple code?
    I think its easier to debug the simple code and not the API function.

    Best Regards,
    ERAN
    Eran Fox
    ASSEMBLER,C,C++,VB6,SQL...

  3. #3

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Other side of town from si_the_geek
    Posts
    7,171

    Re: VB - Search in combobox or listbox for a string

    I haven't benchmarked, but I have read that the API way is faster (and generally API codes are faster)... Never tested though...


    Has someone helped you? Then you can Rate their helpful post.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.