| Option Explicit Private Declare Function SendMessagebyString Lib _ "user32" Alias "SendMessageA" (ByVal hWND As Long, _ ByVal wMsg As Long, ByVal wParam As Long, _ ByVal lParam As String) As Long Private Const LB_FINDSTRINGEXACT = &H1A2 Private Const CB_FINDSTRINGEXACT = &H158 Public Function FindStringinListControl(ListControl As Object, _ ByVal SearchText As String) As Long 'Input: 'ListControl: List or ComboBox Object 'SearchText: String to Search For 'Returns: ListIndex of Item if found 'or -1 if not found Dim lHwnd As Long Dim lMsg As Long On Error Resume Next lHwnd = ListControl.hWND If TypeOf ListControl Is ListBox Then lMsg = LB_FINDSTRINGEXACT ElseIf TypeOf ListControl Is ComboBox Then lMsg = CB_FINDSTRINGEXACT Else FindStringinListControl = -1 Exit Function End If FindStringinListControl = SendMessagebyString _ (lHwnd, lMsg, -1, SearchText) End Function |
Search for a String in a ListBox or ComboBox |
Express News India | Freelance ecommerce web development India