Visual Basic List API Routines
SendMessage: Clearing or Selecting All Items in a List Box
     
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:   VB5, Windows 95
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

SendMessage: Quickly Retrieve Multiple List Box Selections
     
 Prerequisites
None.

This is a one-line API to select or deselect all items in a listbox.

With the controls listed below on the form, set the listbox MultiSelect property to either Simple or Extended. Run the app, and select several items. Click the Command1 to remove the selections, or Command2 to select all the listed items.

 BAS Module Code
None.

 Form Code
Add a listbox and two command buttons to a form, along with the following code:

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_SETSEL = &H185&


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 > 15 Then max = 15

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

   Command1.Caption = "Deselect All"
   Command2.Caption = "Select All"

End Sub


Private Sub Command1_Click()

    Call SendMessage(List1.hwnd, LB_SETSEL, False, ByVal -1)

End Sub


Private Sub Command2_Click()

    Call SendMessage(List1.hwnd, LB_SETSEL, True, ByVal -1)

End Sub
 Comments
When lParam equals -1, setting wParam to False deselects all selections, and setting to True selects all entries. Setting lParam to any other number selects or deselects the index specified, as per the wParam setting.

This message can also be applied against a standard FileList box.


 
 

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