TabIndex 属性示例
以下示例使用 TabIndex 属性显示和设置各个控件的 Tab 键顺序。 可以按 Tab 键访问 Tab 键顺序中的下一个控件,并显示该控件的 TabIndex 。
还可以单击控件以显示其 TabIndex。 可以通过在 TextBox 中指定新的索引值并单击 CommandButton3 来更改控件的 TabIndex。 更改一个控件的 TabIndex 还会更新 Frame 中其他控件的 TabIndex。
若要使用此示例,请将此示例代码复制到窗体的 Declarations 部分。 确保该窗体包含:
- 一个名为"Label1"的 Label 。
- 一个名为"TextBox1"的 TextBox 。
- 一个名为"Frame1"的 Frame 。
- 该 Frame 中一个名为"TextBox2"的 TextBox 。
- Frame 中名为 CommandButton1 和 CommandButton2 的两个 CommandButton 控件。
- Frame 中名为 ScrollBar1 的 ScrollBar。
- 一个名为 CommandButton3 的 CommandButton(不在 Frame 中)。
Private Sub MoveToFront()
Dim i, Temp As Integer
Temp = Frame1.ActiveControl.TabIndex
For i = 0 To Temp - 1
Frame1.Controls.Item(i).TabIndex = i + 1
Next i
Frame1.ActiveControl.TabIndex = 0
TextBox1.Text = Frame1.ActiveControl.TabIndex
End Sub
Private Sub CommandButton3_Click()
Dim i, Temp As Integer
If IsNumeric(TextBox1.Text) Then
Temp = Val(TextBox1.Text)
If Temp >= Frame1.Controls.Count Or Temp < 0
Then
'Entry out of range; move control to front
'of tab order
MoveToFront
ElseIf
Temp > Frame1.ActiveControl.TabIndex
Then
'Move entry down the list
For i = Frame1.ActiveControl.TabIndex + _
1 To Temp
Frame1.Controls.Item(i).TabIndex = _
i - 1
Next i
Frame1.ActiveControl.TabIndex = Temp
TextBox1.Text = _
Frame1.ActiveControl.TabIndex
Else
'Move Entry up the list
For i = Frame1.ActiveControl.TabIndex - _
1 To Temp
Frame1.Controls.Item(i).TabIndex = _
i + 1
Next i
Frame1.ActiveControl.TabIndex = Temp
TextBox1.Text = _
Frame1.ActiveControl.TabIndex
End If
Else
'Text entry; move control to front of tab
'order
MoveToFront
End If
End Sub
Private Sub UserForm_Initialize()
Label1.Caption = "TabIndex"
Frame1.Controls(0).SetFocus
TextBox1.Text = Frame1.ActiveControl.TabIndex
Frame1.Cycle = fmCycleCurrentForm
CommandButton3.Caption = "Set TabIndex"
CommandButton3.TakeFocusOnClick = False
End Sub
Private Sub TextBox2_Enter()
TextBox1.Text = Frame1.ActiveControl.TabIndex
End Sub
Private Sub CommandButton1_Enter()
TextBox1.Text = Frame1.ActiveControl.TabIndex
End Sub
Private Sub CommandButton2_Enter()
TextBox1.Text = Frame1.ActiveControl.TabIndex
End Sub
Private Sub ScrollBar1_Enter()
TextBox1.Text = Frame1.ActiveControl.TabIndex
End Sub
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。