Visual Basic List API Routines
SendMessage: Quickly Find List Box Items using Full or Partial Matching
     
Posted:   Wednesday January 8, 1997
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6, and VB3, VB4-16 with appropriate declarations
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   VBnet - Randy Birch
     
 Prerequisites
None.

There may be times when, through code, you wish to select or highlight a specific entry in a listbox. Using the SendMessage with either LB_FINDSTRING or LB_FINDSTRINGEXACT, this is easily accomplished. The demo shows locating a string as the user types .. the API can also be used standalone when determining where in a listbox a particular string is located.
 BAS Module Code
None.

 Form Code
Toss a text box and a listbox onto a form, and add the following code to the form:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Declare Function SendMessage Lib "user32" _
    Alias "SendMessageA" _ 
    (ByVal hwnd As Long, _ 
     ByVal wMsg As Long, _ 
     ByVal wParam As Long, _ 
     lParam As Any) As Long

Private Const LB_FINDSTRING = &H18F
Private Const LB_FINDSTRINGEXACT = &H1A2


Private Sub Form_Load()

  'add this code to the form load to 
  'fill a list with some (15) of the 
  'system's screen fonts.
   Dim i As Integer
   Dim max As Integer

   max = Screen.FontCount
   If max > 50 Then max = 50

   For i = 1 To max
      List1.AddItem Screen.Fonts(i)
   Next

End Sub


Private Sub Text1_Change()

    On Error Resume Next
    
   'find the first match beginning at the 
   'start of the list. To specify searching 
   'should begin with a specific list item, 
   'change wParam from -1 to a value 
   'between 0 and List1.ListCount -1. 
    List1.ListIndex = SendMessage(List1.hwnd, _
                                         LB_FINDSTRING, _
                                         -1, _
                                         ByVal (Text1.Text))

End Sub
 Comments
Run the project, and begin to type a font name into the textbox. If there is a font beginning with the entered text, it will be selected.

The return value of the call is the ListIndex of the matching item, or -1 if a match is not found. Remember too that whenever code sets the ListIndex property, it causes a List_Click event to fire. If this is undesirable, appropriate flags need to be implemented.

If the constant used in the API call is LB_FINDSTRING, any partial match *beginning* with the entered string is located. If the constant LB_FINDSTRINGEXACT is used, then the entered text much match exactly the string in the listbox. LB_FINDSTRINGEXACT is most useful when attempting to determine if the precise string specified is in a list.

Passing Text1.Text in brackets causes the variant data containing the text property to be coerced into a string data type for passing ByVal without error. If Text1.Text is cast to a string variable instead, the brackets around the variable are unneeded, however the parameter still must be passed to the API ByVal.


 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter