I made a form with 10 listboxes. I want to give focus to a listbox of my choosing. For example, if i choose the integer “3” it will put focus on the listbox whose tabindex = 3.
How do I do this?
I thought this might work but it doesn’t:
Dim lst As VB.ListBox: Set lst = Form.Controls(lst.TabIndex = 3)
lst.SetFocus
Do you have a control array for your list boxes? If so, you can simply do this:
The above code will only work properly for you if the List boxes are a control array and you have the tab indexes set in the same order that you placed the list boxes on the screen.
Otherwise, you can try this code:
This code will loop through all the controls on your form and actively search for the one that matches your tab index. Once the control is found, it sets the focus to it.