Vb6 我如何选择&;在VB中仅循环浏览列表框(忽略所有其他控件)?

Vb6 我如何选择&;在VB中仅循环浏览列表框(忽略所有其他控件)?,vb6,listbox,controls,Vb6,Listbox,Controls,我的表单有很多控件,比如命令按钮和列表框。我创建了一个“左”和“右”按钮来循环浏览控件,但后来我意识到我真的只想循环浏览列表框,忽略所有其他不是列表框的控件。这是我的代码,但我现在意识到它在所有控件中循环,包括命令按钮、文本框和列表框。如何使其忽略除列表框以外的所有控件。基本上,我让这些L和R按钮只在列表框中循环,就像使用Tab和Ctrl+Tab来回循环一样 Private Sub FocusListBoxByTabIndex(offset As Long) Dim ctrl As VB.Con

我的表单有很多控件,比如命令按钮和列表框。我创建了一个“左”和“右”按钮来循环浏览控件,但后来我意识到我真的只想循环浏览列表框,忽略所有其他不是列表框的控件。这是我的代码,但我现在意识到它在所有控件中循环,包括命令按钮、文本框和列表框。如何使其忽略除列表框以外的所有控件。基本上,我让这些L和R按钮只在列表框中循环,就像使用Tab和Ctrl+Tab来回循环一样

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  

这在VB6中是不可能的。如果这是由于性能问题(表单上的许多控件),那么有一些方法可以提高效率。您可以创建一个列表框控件的数组/集合,并在该数组/集合中循环。

这是可行的,但前提是您记住哪个控件具有当前焦点。 因此,如果还使用鼠标或制表符循环控件,则需要每个控件使用_GotFocus事件,然后设置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
Private CurTabIndex作为整数
专用子表单_加载()
CurTabIndex=0
端接头
专用子焦点ListBoxByTabIndex(按长度偏移)
将ctrl键设置为VB.Control
控件作为VB.Control
对于我的每一个ctrl
如果ctrl的类型为ListBox,则
如果偏移量>0,则
如果ctrl.TabIndex>=CurTabIndex+偏移量,则
如果FirstControl什么都不是,那么
设置FirstControl=ctrl
ElseIf FirstControl.TabIndex>ctrl.TabIndex然后
设置FirstControl=ctrl
如果结束
如果结束
其他的

如果ctrl.TabIndex不能将列表框创建为控件数组吗

listbox(offset).setfocus