Set the ListIndex Without the Click Event

If you set the ListIndex property of a list-box or combo-box control, VB might generate an unwanted Click event. Instead of writing code to bypass the Click event, use SendMessage to set the ListIndex without generating the event. Call the SetListIndex function below, passing the list (either a list box or combo box) and the desired new index value. SetListIndex attempts to set the value and returns the current index so you can confirm whether your request “took.” For example, this code should set the index to the tenth element:

 	Debug.Print SetListIndex(List1, 9)

If an error occurred (if there were only eight elements, for example), the previous index value is returned. Code the SetListIndex function in a standard module:

 Private Declare Function SendMessage Lib _	"user32" Alias "SendMessageA" (ByVal _	hWnd As Long, ByVal wMsg As Long, ByVal _	wParam As Long, lParam As Any) As LongPublic Function SetListIndex(lst As Control, _	ByVal NewIndex As Long) As Long	Const CB_GETCURSEL = &H147	Const CB_SETCURSEL = &H14E	Const LB_SETCURSEL = &H186	Const LB_GETCURSEL = &H188	If TypeOf lst Is ListBox Then		Call SendMessage(lst.hWnd, _			LB_SETCURSEL, NewIndex, 0&)		SetListIndex = SendMessage(lst.hWnd, _			LB_GETCURSEL, NewIndex, 0&)	ElseIf TypeOf lst Is ComboBox Then		Call SendMessage(lst.hWnd, _			CB_SETCURSEL, NewIndex, 0&)		SetListIndex = SendMessage(lst.hWnd, _			CB_GETCURSEL, NewIndex, 0&)	End IfEnd Function
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

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

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