Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Dec 9, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 Long

Public Function SetListIndex(lst As Control, _
	ByVal NewIndex As Long) As Long
	Const CB_GETCURSEL = &H147
	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 If
End Function
Greg Ellis
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date