Practical Web Programming

Tuesday, January 15, 2008

Automatically Search ListBox Using API in Visual Basic

Using API, this codes automatically searches the listbox for whatever you type in the textbox.

To use this codes, add a ListBox, named List1 and a TextBox, named Text1 into your project and copy and paste the codes to your project.

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

Const LB_FINDSTRING = &H18F

Private Sub Form_Load()
With List1
.AddItem "Computer"
.AddItem "Screen"
.AddItem "Modem"
.AddItem "Printer"
.AddItem "Scanner"
.AddItem "Sound Blaster"
.AddItem "Keyboard"
.AddItem "CD-Rom"
.AddItem "Mouse"
End With
End Sub

Private Sub Text1_Change()
Index = SendMessage(List1.hwnd, LB_FINDSTRING, -1, _
ByVal CStr(Text1.Text))
If Index < 0 Then Exit Sub
List1.ListIndex = Index
Text1.Text = List1.List(Index)
End Sub

0 comments:

Recent Post