Reputation: 4434
My form has a bunch of controls like command buttons and listboxes. I created a "left" and "right" button to cycle through the controls, but then I realized that I really only want to cycle through listboxes, ignoring all other controls that are not listboxes. Here's my code, but I realize now that it cycles through ALL controls, both command buttons, text boxes, AND listboxes. How do I make it so that it ignores all controls EXCEPT listboxes. Essentially, I am making these L and R buttons cycle through only listboxes, kind of like using Tab and Ctrl+Tab to cycle back and forth.
Private Sub FocusListBoxByTabIndex(offset As Long)
Dim ctrl As VB.Control
For Each ctrl In Me
If TypeOf ctrl Is ListBox Then
If ctrl.TabIndex = lastFocus.TabIndex + offset Then
ctrl.SetFocus
Exit Sub
End If
End If
Next
End Sub
Private Sub Command2_Click() 'left button
FocusListBoxByTabIndex -1
End Sub
Private Sub Command3_Click() 'right button
FocusListBoxByTabIndex 1
End Sub
Upvotes: 0
Views: 304
Reputation: 870
can't you create your listboxes as a control array
listbox(offset).setfocus
Upvotes: 0
Reputation: 1213
This works, but only if you remember what control has current focus. So if you also use mouse or tab to cycle controls you need each control to use the _GotFocus event and then set CurTabIndex.
Private CurTabIndex As Integer
Private Sub Form_Load()
CurTabIndex = 0
End Sub
Private Sub FocusListBoxByTabIndex(offset As Long)
Dim ctrl As VB.Control
Dim FirstControl As VB.Control
For Each ctrl In Me
If TypeOf ctrl Is ListBox Then
If offset > 0 Then
If ctrl.TabIndex >= CurTabIndex + offset Then
If FirstControl Is Nothing Then
Set FirstControl = ctrl
ElseIf FirstControl.TabIndex > ctrl.TabIndex Then
Set FirstControl = ctrl
End If
End If
Else
If ctrl.TabIndex <= CurTabIndex + offset Then
If FirstControl Is Nothing Then
Set FirstControl = ctrl
ElseIf FirstControl.TabIndex < ctrl.TabIndex Then
Set FirstControl = ctrl
End If
End If
End If
End If
Next
If Not FirstControl Is Nothing Then
CurTabIndex = FirstControl.TabIndex
FirstControl.SetFocus
End If
End Sub
Private Sub Command2_Click() 'left button
FocusListBoxByTabIndex -1
End Sub
Private Sub Command3_Click() 'right button
FocusListBoxByTabIndex 1
End Sub
Upvotes: 2
Reputation: 12804
This is not possible in VB6. If this is due to a performance hit (MANY controls on form), there are ways to make it more efficient. You could create an array/collection of listbox controls and cycle through that array/collection.
Upvotes: 0