Add Multicharacter Search Capability to Listboxes

Users often complain that listboxes don’t have the same multiple keypress search capabilities as treeviews and other objects. But you can simulate this behavior by adding code to a form with a timer and a listbox whose Sorted property is set to True.

For this test, Form_Load adds some data and sets the default interval between keystrokes. You can type in “AL” to get to Allan instead of the first instance of an entry with an “a” in the list. This can be extremely helpful in long lists. You can also convert this code easily for use within a custom control:

Option ExplicitPrivate Declare Function SendMessage Lib "user32" _	Alias "SendMessageA" (ByVal hwnd As Long, _	ByVal wMsg As Long, ByVal wParam As Long, _	lParam As Any) As LongPrivate Const LB_FINDSTRING = &H18FPrivate Const LB_ERR = (-1)Private sSearchstring As StringPrivate Sub Form_Load()	With List1		.AddItem "Adam"		.AddItem "Allan"		.AddItem "Arty"		.AddItem "Aslan"		.AddItem "Barney"		.AddItem "Bob"	End With	Timer1.Interval = 2000End SubPrivate Sub List1_KeyPress(KeyAscii As Integer)	Dim nResult As Long	Timer1.Enabled = True	sSearchstring = sSearchstring & Chr$(KeyAscii)	With List1		nResult = SendMessage(.hWnd, LB_FINDSTRING, _			.ListIndex, ByVal sSearchstring)		If nResult <> LB_ERR Then			.ListIndex = nResult			KeyAscii = 0		End If	End WithEnd SubPrivate Sub Timer1_Timer()	sSearchstring = ""	Timer1.Enabled = FalseEnd Sub
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

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