Find a string in a list box

Some times you may need to find the listindex of an item in a list box. To do this you need not loop thru all the items in the listbox, you can do this very fast using SendMessage API.

'**Include the following lines in the declaration part

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Long, lParam As Any) As Long

Const LB_FINDSTRING = &H18F

'**Include the following code in any of the event from where you want to call SendMessage
Dim lReturnVal as long
Dim GivenStr as String

'Set some string which is in the List box
GivenStr = "Test"
lReturnVal = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal GivenStr)

In the above statement, if the function is successful 'lReturnVal' will return the listindex of the string found. So you can set the listindex property for the list box to make the item selected.