设置框架中控件的 Tab 键顺序

以下示例使用 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 支持和反馈,获取有关如何接收支持和提供反馈的指南。