以下範例使用 TabIndex 屬性來顯示並設定各個控制項的分頁順序。 你可以按 Tab 鍵進入 tab 順序中的下一個控制項,並顯示該控制項的 TabIndex 。
你也可以點擊控制項來顯示它的 TabIndex。 你可以在 TextBox 指定新的索引值,然後點選 CommandButton3,來更改控制項的 TabIndex。 更改一個控制項的 TabIndex 也會更新框架中其他控制項的 TabIndex。
若要使用本範例,請將此範例程式碼複製到表單的宣告部分中。 請確定該表單包含:
- 一個名為 Label1 的 Label 。
- 一個名為 TextBox1 的 TextBox 。
- 一個名為 Frame1 的 Frame 。
- 該 Frame 中一個名為 TextBox2 的 TextBox 。
- 幀中有兩個 CommandButton 控制 項,分別 是 CommandButton1 和 CommandButton2。
- 畫面中有一條名為 ScrollBar1 的捲軸條。
- 一個名為 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 支援與意見反應。