# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  VB - Search in combobox or listbox for a string

## manavo11

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:
Option Explicit
Private Const CB_FINDSTRING = &H14C
Private Const LB_FINDSTRING = &H18F
Private Declare Function SendMessage Lib _
    "user32" Alias "SendMessageA" (ByVal _
    hwnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, lParam As Any) _
    As Long
 Private Sub Command1_Click()
    FindCB Combo1, Text1.Text
End Sub
 Private Sub Command2_Click()
    FindLB List1, Text1.Text
End Sub
 Private Sub Form_Load()
    Command1.Caption = "Find in Combo Box"
    Command2.Caption = "Find in List Box"
     Combo1.AddItem "one"
    Combo1.AddItem "two"
    Combo1.AddItem "three"
    Combo1.AddItem "four"
    Combo1.AddItem "five"
    Combo1.AddItem "six"
    Combo1.AddItem "seven"
     List1.AddItem "one"
    List1.AddItem "two"
    List1.AddItem "three"
    List1.AddItem "four"
    List1.AddItem "five"
    List1.AddItem "six"
    List1.AddItem "seven"
End Sub
 Private Sub FindLB(obj As Object, TextToFind As String)
    obj.ListIndex = SendMessage( _
       obj.hwnd, LB_FINDSTRING, -1, ByVal _
       TextToFind)
End Sub
 Private Sub FindCB(obj As Object, TextToFind As String)
    obj.ListIndex = SendMessage( _
       obj.hwnd, CB_FINDSTRING, -1, ByVal _
       TextToFind)
End Sub

----------


## eranfox

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

cant you just search it like this?


VB Code:
Public Function CheckIfExistInCombo(objCombo As Object,TextToFind As String) As Boolean
    Dim NumOfItems As Variant 'The Number Of Items In ComboBox
    Dim IndexNum As Integer 'Index
    
    NumOfItems = objCombo.ListCount
    For IndexNum = 0 To NumOfItems - 1
        If objCombo.List(IndexNum) = TextToFind.Text Then
            CheckIfExistInCombo = True
            Exit Function
        End If
    Next IndexNum
    
    CheckIfExistInCombo = False
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

----------


## manavo11

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

----------

