Enter 和 Exit 事件、ActiveControl 属性示例Enter, Exit events, ActiveControl property example

下面的示例在跟踪用户访问的控件的子例程中使用**ActiveControl** 属性。The following example uses the ActiveControl property in a subroutine that tracks the controls a user visits. 每个控件的**Enter** 事件调用 TraceFocus 子例程以标识具有焦点的控件。The Enter event for each control calls the TraceFocus subroutine to identify the control that has the focus.

若要使用此示例,请将此示例代码复制到窗体的 Declarations 部分。To use this example, copy this sample code to the Declarations portion of a form. 确定窗体包含以下控件:Make sure that the form contains the following controls:

  • 一个名为"ScrollBar1"的 ScrollBarA ScrollBar named ScrollBar1.
  • 一个名为"ListBox1"的 ListBoxA ListBox named ListBox1.
  • 两个名称分别为"OptionButton1"和"OptionButton2"的 OptionButton 控件。Two OptionButton controls named OptionButton1 and OptionButton2.
  • 一个名为 Frame1 的**框架**。A Frame named Frame1.
Dim MyControl As Control 
 
Private Sub TraceFocus() 
 ListBox1.AddItem ActiveControl.Name 
 ListBox1.List(ListBox1.ListCount - 1, 1) = _ 
 ActiveControl.TabIndex 
End Sub 
 
Private Sub UserForm_Initialize() 
 ListBox1.ColumnCount = 2 
 ListBox1.AddItem "Controls Visited" 
 ListBox1.List(0, 1) = "Control Index" 
End Sub 
 
Private Sub Frame1_Enter() 
 TraceFocus 
End Sub 
 
Private Sub ListBox1_Enter() 
 TraceFocus 
End Sub 
 
Private Sub OptionButton1_Enter() 
 TraceFocus 
End Sub 
 
Private Sub OptionButton2_Enter() 
 TraceFocus 
End Sub 
 
Private Sub ScrollBar1_Enter() 
 TraceFocus 
End Sub

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.