ActiveControl Property

       

Returns the control that has the focus. When a form is referenced, as in ChildForm.ActiveControl, ActiveControl specifies the control that would have the focus if the referenced form were active. Not available at design time; read-only at run time.

Syntax

object.ActiveControl

The object placeholder represents an object expression that evaluates to an object in the Applies To list.

Remarks

You can use ActiveControl to access a control's properties or to invoke its methods: For example, Screen.ActiveControl.Tag = "0". A run-time error occurs if all controls on the form are invisible or disabled.

Each form can have an active control (Form.ActiveControl), regardless of whether or not the form is active. You can write code that manipulates the active control on each form in your application even when the form isn't the active form.

This property is especially useful in a multiple-document interface (MDI) application where a button on a toolbar must initiate an action on a control in an MDI child form. When a user clicks the Copy button on the toolbar, your code can reference the text in the active control on the MDI child form, as in ActiveForm.ActiveControl.SelText.

Note   If you plan to pass Screen.ActiveControl to a procedure, you must declare the argument in that procedure with the clause As Control rather than specifying a control type (As TextBox or As ListBox) even if ActiveControl always refers to the same type of control.