HOWTO: Search a ListBox Control Quickly

Last reviewed: July 15, 1997
Article ID: Q161161
The information in this article applies to:
  • Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, version 5.0

SUMMARY

A popular item in a user interface is to "link" a text box to a list box so the nearest match in the list box is selected when the user types text into the text box. This technique can be implemented using pure Visual Basic code, but the Windows API provides a quicker and easier way to do this.

MORE INFORMATION

The technique calls the Windows API SendMessage function using the LB_FINDSTRING message for a list box to locate a partial match for a string in the list box. SendMessage requires the following parameters:

      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.

Step-by-Step Example

  1. Start a new Standard EXE project. Form1 is added by default.

  2. Add a TextBox control (Text1) and a ListBox control (List1) to Form1.

  3. Add the following code to the General Declarations section of Form1:

          Const LB_FINDSTRING = &H18F
          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
    
          Private Sub Form_Load()
             List1.Clear
             List1.AddItem "Apples"
             List1.AddItem "Banana"
             List1.AddItem "Bread"
             List1.AddItem "Break"
             Text1.Text = ""
          End Sub
    
          Private Sub Text1_Change()
             List1.ListIndex = SendMessage(List1.hWnd, LB_FINDSTRING, -1, _
                ByVal CStr(Text1.Text))
          End Sub
    
    

  4. Press the F5 key to run the program. Typing text into the text box selects the first item in the list box which matches the text in the text box.
Keywords          : vb5all vb5howto VBKBCtrl VBKBInt VBKBListBox VBKBStd kbprg kbhowto
Version           : 5.0
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


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: July 15, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.