Speed up Selecting Items in a MultiSelect List Box

Try this routine to select or deselect all items in a list box. Add a list box to your form with the MultiSelect property set to Simple or Extended, and add items to the list box:

 Sub ToggleSelectAll (lst As ListBox, ByVal bState As Integer)	Dim i As Integer	lst.Visible = False	'Avoid redraws for each item	For i = 0 To (lst.ListCount - 1)		lst.Selected(i) = bState	Next i	lst.Visible = TrueEnd Sub

This routine sets the list box’s Visible property to False to prevent the list box from repainting itself after each item is selected. This makes the routine more efficient. However, you can speed up this routine by using SendMessage instead of VB statements:

 Sub ToggleSelectAll (lst As ListBox, _	ByVal bState As Integer)	Dim i As Integer	Dim nRet As Long	lst.Visible = False	For i = 0 To lst.ListCount - 1		nRet = SendMessage(lst.hWnd, LB_SETSEL, bState, i)	Next i	lst.Visible = TrueEnd Sub

You can speed up this routine significantly by taking advantage of the fact that the LB_SETSEL message sets the state of all items in the list box at once if the index argument is -1. Because this approach uses a single call to SendMessage, you don’t need to worry about setting the list box’s Visible property to False to prevent multiple redraws:

 Sub ToggleSelectAll (lst As ListBox, _	ByVal bState As Integer)	Dim nRet As Long	nRet = SendMessage(lst.hWnd, LB_SETSEL, bState, -1)End Sub

Use these 16-bit declarations:

 Declare Function SendMessage Lib "User" ( _	ByVal hWnd As Integer, ByVal wMsg As Integer, _	ByVal wParam As Integer, ByVal lParam As Long) As LongGlobal Const WM_USER = &H400Global Const LB_SETSEL = (WM_USER + 6)

Or use these 32-bit declarations:

 Public Declare Function SendMessage Lib _	"User32" Alias "SendMessageA" ( _	ByVal hWnd As Long, ByVal wMsg As Integer, _	ByVal wParam As Long, ByVal lParam As Long) As LongPublic Const WM_USER = &H400Public Const LB_SETSEL = (WM_USER + 6)
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.