How to Quickly Search a List Box

Last reviewed: January 10, 1996
Article ID: Q141027
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0

SUMMARY

A popular item in a user interface is to "link" a text box to a list box so that as the user types text into the text box, the nearest match in the list box is selected. Although this technique can be implemented fairly easily using pure Visual Basic code, the Windows API provides a quick and easy way of doing this.

MORE INFORMATION

The following technique uses the LB_FINDSTRING message for a list box to locate a partial match for a string in a list box. To do this, call the Windows API SendMessage function. The SendMessage function requires the following parameters to list the files:

SendMessage   (hWnd,LB_FINDSTRING, wParam, lparam)

where

hWnd    is the hWnd of the list box.
wParam is an integer that specifies the starting point for the search.
          Use -1 to search the whole list box.
Lparam   is a long pointer to the string to find.

Example to Demonstrate Searching a List Box

  1. Start a new project in Visual Basic. Add a text box (Text1) and a list box (list1) to Form1.

  2. On the Visual Basic Insert menu, choose module to add a new code module to the project. Add the following code to the General declarations section of module1:

          #If Win32 Then
    
             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
          #Else
             Declare Function SendMessage Lib "user" (ByVal hwnd As Integer, _
             ByVal wMsg As Integer, ByVal wp As Integer, lp As Any) As Long
          #End If
    
          Public Const LB_FINDSTRING = &H18F
    
    

  3. In the Form_Load method for the form, add the following items to the list box using the following code:

          Sub Form_Load()
             List1.Clear
             List1.AddItem "Apples"
             List1.AddItem "Banana"
             List1.AddItem "Bread"
             List1.AddItem "Break"
          End Sub
    
    

  4. In the Change method of the text box, add the following code:

          Sub Text1_Change()
             List1.ListIndex = SendMessage(List1.hWnd, LB_FINDSTRING, -1, _
             ByVal Cstr(Text1.Text))
          End Sub
    
    
If you run the code, typing text into the text box selects the first item in the list that matches the text in the text box.


Additional reference words: 4.00 vb4win vb4all
KBCategory: kbprg kbcode kbhowto
KBSubCategory: APrgDataAcc


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 10, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.