VB6 Tip: Achieve a better user experience with improved auto-find in a List Box
Takeaway: Improve user experience inside a List Box with code that will expand the functionality of the built-in auto find features of Visual Basic 6.
By Peter Aitken
This article originally appeared in the Builder.com Visual Basic e-newsletter. Click here to subscribe automatically.
When a List Box control has the focus, it will automatically scroll to the first item that begins with the letter you type. It will not, however, automatically select items based on more than the first letter. For example, if you type ap, you may want the control to select the first item that begins with ap. This tip explains how you can implement this behavior using the API function SendMessage. Its declaration, which must be included in the program, is as follows:
Public Declare Function SendMessage Lib "user32" _Alias "SendMessageA" (ByValhwnd As Long, _
ByValwMsg As Long, ByValwParam As Long, _
ByVallParam As String) As Long
You'll also need the following constant, which tells the List Box control to select the first item that begins with the specified prefix:
Public Const LB_SELECTSTRING = &H18CRelated resources
Enhancing
form usability with instructions and validation
Download: vbDevelopment Tools
Webcast: Developing cross-platform VB applications for
handhelds
Get control
In addition to the List Box, this technique requires a Text Box control. The user enters text in the Text Box, and the List Box automatically selects the first matching item. The message is sent in the Text Box's Change event procedure:
Private Sub Text1_Change()If Text1.Text <> "" Then
SendMessage List1.hwnd, LB_SELECTSTRING, -1, Text1.Text
End If
End Sub
Letting the user select List Box items based on more than just the first letter can be particularly useful when the list contains many items.
Peter Aitken has been programming with Visual Basic since Version 1.0. He has written numerous books and magazine articles on Visual Basic and other computer and programming topics.
SponsoredWhite Papers, Webcasts, and Downloads
- TCP/IP Troubleshooting Global Knowledge
- The New Generation of Microsoft Certifications: What's at the Core? Global Knowledge
- The OSI Model: Understanding the Seven Layers of Computer Networks Global Knowledge
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET






