将 ActiveControl 属性与 Screen 对象结合使用,以标识或引用具有焦点的控件。 只读的 控件 对象。
语法
表达式。ActiveControl
表达式 表示 Screen 对象的变量。
备注
该属性设置包含对在运行时具有焦点的 控件 对象的引用。
使用 ActiveControl 属性引用在运行时具有焦点的控件及其某个属性或方法。 下面的示例对 strControlName变量赋以具有焦点的控件的名称。
Dim ctlCurrentControl As Control
Dim strControlName As String
Set ctlCurrentControl = Screen.ActiveControl
strControlName = ctlCurrentControl.Name
如果没有控件具有焦点,当您使用 ActiveControl 属性,或者隐藏或禁用的所有活动窗体的控件,将发生错误。
示例
下面的示例将活动控件 ctlCurrentControl 分配给变量,然后根据控件的 Name 属性的值执行不同的操作。
Dim ctlCurrentControl As Control
Set ctlCurrentControl = Screen.ActiveControl
If ctlCurrentControl.Name = "txtCustomerID" Then
.
. ' Do something here.
.
ElseIf ctlCurrentControl.Name = "btnCustomerDetails" Then
.
. ' Do something here.
.
End If
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。