Practical Web Programming

Thursday, January 03, 2008

Visual Basic: How To Automatically Search Text in a Listbox

This codes searches the list for whatever you type in the textbox that matches the text in it.

To use this codes, add a ListBox, named List1, and a TextBox, named Text1 into your project and copy and paste the codes bleow 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

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


Recent Post