共用方式為


設定框架中控制項的定位順序

下列範例使用 TabIndex 屬性來顯示和設定個別控制項的 TAB 鍵順序。 TabIndex屬性是Microsoft Forms 2.0 屬性,適用于可存在於 Frame 中的每個控。 使用者可以按下 TAB 鍵到達 Tab 鍵順序中的下一個控制項,並且顯示該控制項的 TabIndex 屬性。 使用者也可以按一下 TextBoxScrollBar以外的任何控制項,以顯示其 TabIndex。 使用者可以透過在 TextBox 中指定新索引值並按一下 CommandButton3,來變更控制項的 TabIndex 屬性。 變更其中一個控制項的 TabIndex 屬性,將同時更新 Frame 中其他控制項的 TabIndex 屬性。

若要使用本範例,請將此範例程式碼複製到表單的指令碼編輯器中。 若要執行該程式碼,您需要開啟該表單以啟動 Open 事件。 請確定表單包含:

  • 一個名為 Label1 的 Label

  • 一個名為 TextBox1 的 TextBox

  • 一個名為 Frame1 的 Frame

  • Frame 中一個名為 TextBox2 的 TextBox

  • Frame中名為CommandButton1 和 CommandButton2的兩個 CommandButton 控制項。

  • Frame 中一個名為 ScrollBar1 的 ScrollBar

  • 一個名為 CommandButton3 的 CommandButton (不在該 Frame 中)。

Sub MoveToFront() 
 Set Frame1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("Frame1") 
 Set TextBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("TextBox1") 
 
 Temp = Frame1.ActiveControl.TabIndex 
 For i = 0 To Temp - 1 
 Frame1.Controls.Item(i).TabIndex = i + 1 
 Next 
 
 Frame1.ActiveControl.TabIndex = 0 
 TextBox1.Text = Frame1.ActiveControl.TabIndex 
End Sub 
 
Sub CommandButton3_Click() 
 Set Frame1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("Frame1") 
 Set TextBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("TextBox1") 
 
 If IsNumeric(TextBox1.Text) Then 
 Temp = CInt(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 
 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 
 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 
 
Sub Item_Open() 
 Set Frame1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("Frame1") 
 Set TextBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("TextBox1") 
 Set Label1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("Label1") 
 Set CommandButton3 = Item.GetInspector.ModifiedFormPages("P.2").Controls("CommandButton3") 
 
 Label1.Caption = "TabIndex" 
 
 Frame1.Controls(0).SetFocus 
 TextBox1.Text = Frame1.ActiveControl.TabIndex 
 
 Frame1.Cycle = 2 '2=fmCycleCurrentForm 
 
 CommandButton3.Caption = "Set TabIndex" 
 CommandButton3.TakeFocusOnClick = False 
End Sub 
 
Sub CommandButton1_Click() 
 Set Frame1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("Frame1") 
 Set TextBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("TextBox1") 
 
 TextBox1.Text = Frame1.ActiveControl.TabIndex 
End Sub 
 
Sub CommandButton2_Click() 
 Set Frame1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("Frame1") 
 Set TextBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("TextBox1") 
 
 TextBox1.Text = Frame1.ActiveControl.TabIndex 
End Sub

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應