使用 ActiveControl 屬性可以和 Screen 對(duì)象一起來(lái)識(shí)別或引用獲得焦點(diǎn)的控件。Control 對(duì)象,只讀。
expression 必需。返回“應(yīng)用于”列表中的一個(gè)對(duì)象的表達(dá)式。
此屬性設(shè)置中包含對(duì)在運(yùn)行時(shí)獲得焦點(diǎn)的 Control 對(duì)象的引用。
該屬性僅在使用宏或 Visual Basic 時(shí)才可用,并且在所有視圖中具有只讀屬性。
可以使用 ActiveControl 屬性來(lái)引用在運(yùn)行時(shí)與其屬性或方法之一共同獲得焦點(diǎn)的控件。以下示例將獲得焦點(diǎn)的控件名稱指定給 strControlName 變量:
Dim ctlCurrentControl As Control
Dim strControlName As String
Set ctlCurrentControl = Screen.ActiveControl
strControlName = ctlCurrentControl.Name
如果在使用 ActiveControl 屬性時(shí)沒有控件獲得焦點(diǎn),或所有窗體中的活動(dòng)控件都是隱藏或失效的,則會(huì)產(chǎn)生錯(cuò)誤。
下面的示例將活動(dòng)控件指定給 ctlCurrentControl 變量,然后根據(jù)不同的控件 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