devxlogo

Add Multicharacter Search Capability to Listboxes

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
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist