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




Reply With Quote