AllAPI Network - The KPD-Team

 
Allapi Network
 API-Guide
 ApiViewer

 API List

 
API Resources
 Tips & Tricks
 VB Tutorials
 Error Lookup
 
Misc Stuff
 VB examples
 VB Tools
 VB Links
 Top Downloads
 
This Site
 Search Engine
 Contact Form
 

Donate to AllAPI.net

How to search a list box by typing text into a control

The second most popular questions that I received during the past week is how to search a list box by typing text into a control such as a textbox or combobox. This is very simple to do and uses the SendMessage API. This works with VB 4, 5 and 6

Here is the example:

  • Start a new Standard-EXE project.
  • Add a command button, textbox and a listbox control to form 1
  • Add the following code to form1:
Option Explicit

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

Const LB_FINDSTRING = &H18F

Private Sub Command1_Click()
End
End Sub

Private Sub Form_Load()
With List1
  .Clear
  .AddItem "CPU"
  .AddItem "RAM"
  .AddItem "ROM"
  .AddItem "Cache"
  .AddItem "Motherboard"
  .AddItem "Hard Disk"
  .AddItem "Floppy Disk"
End With
End Sub

Private Sub Text1_Change()
List1.ListIndex = SendMessage(List1.hWnd, _
LB_FINDSTRING, -1, ByVal Text1.Text)
Text1.Text = List1.Text
End Sub

  • Run the project (F5) and type some text into the text box, and if it matches the text in the listbox then the text will be displayed in the text box. You can do this will any control such as a combo box that allows you to enter text into it and has a text property.

 

 


Copyright © 1998-2007, The Mentalis.org Team - Privacy statement
Did you find a bug on this page? Tell us!
This site is located at http://allapi.mentalis.org/