Set the ListIndex Without the Click Event

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of