See Also Example1 Example2
Applies To
Form, MDI form
Description
Specifies the control that has the focus ; when a form is referenced, as in ChildForm.ActiveControl, specifies the control that would have the focus if the referenced form were active. Invalid if all controls on the given form are invisible or disabled. Not available at design time; read-only at run time.
Usage
{ form.|mdiform.|Screen.}ActiveControl
Remarks
You can use ActiveControl to access a control's properties or to invoke its methodsfor example, Screen.ActiveControl.Tag = "0".
When a control has the focus, it is the active control on the screen (Screen.ActiveControl). In addition, each form can have a control that is the active control on that form (Form.ActiveControl), regardless of whether or not the form is active. You can, therefore, write code that manipulates the active control on each form in your application, even when the form is not the active form.
This property is especially useful in an MDI application where a button on a toolbar must initiate an action on a control in an MDI child form. When a user clicks a Copy button on the toolbar, your code can reference the text in the active control on the MDI child formfor example, ActiveForm.ActiveControl.SelText.
Note If you plan to pass Screen.ActiveControl to a procedure, you must declare the argument in that procedure with the generic type (As Control) rather than a specific form type (As TextBox or As ListBox)even if ActiveControl always refers to the same type of control.
Data Type
Control
Help:
ActiveForm Property
GotFocus Event
LostFocus Event
Programmer's Guide:
Chapter 3, "Creating and Using Controls"
Chapter 14, "Multiple-Document Interface (MDI) Applications"
ActiveControl Property Example 1
The example displays the text of the active control. To try this example, paste the code into the Declarations section of a form that contains a text box, a label, and a command button. Then press F5 and click the form.
Sub Form_Click ()
If TypeOf Screen.ActiveControl Is TextBox Then
Label1.Caption = Screen.ActiveControl.Text
Else
Label1.Caption = "Button: " + Screen.ActiveControl.Caption
End If
End Sub
ActiveForm, ActiveControl Properties Example 2
The example shows how you can use the Clipboard in cut, copy, paste, and delete operations using buttons on a toolbar. To try this example, draw a text box and a check box on Form1, then create a new MDI form. On the MDI form, draw a picture box and then draw a command button in the picture box. Set the Index property on the command button to 0 (this creates a control array). On Form1, set the MDIChild property to True.
To run the example, copy the code into the Declarations section of the MDI form and press F5. Notice that if the check box has the focus, the buttons don't work.
Sub MDIForm_Load ()
Dim I ' Declare variable.
Command1(0).Move 0, 0, 700, 300 ' Position button on toolbar.
For I = 1 To 3 ' Create other buttons.
Load Command1(I) ' Create button.
Command1(I).Move I * 700, 0, 700, 300 ' Place and size button.
Command1(I).Visible = True ' Display button.
Next I
Command1(0).Caption = "Cut" ' Set button captions.
Command1(1).Caption = "Copy"
Command1(2).Caption = "Paste"
Command1(3).Caption = "Del"
End Sub
Sub Command1_Click (Index As Integer)
' ActiveForm refers to the active form in the MDI form.
If TypeOf ActiveForm.ActiveControl Is TextBox Then
Select Case Index
Case 0 ' Cut.
' Copy selected text to Clipboard.
Clipboard.SetText ActiveForm.ActiveControl.SelText
' Delete selected text.
ActiveForm.ActiveControl.SelText = ""
Case 1 ' Copy.
' Copy selected text to Clipboard.
Clipboard.SetText ActiveForm.ActiveControl.SelText
Case 2 ' Paste.
' Put Clipboard text in text box.
ActiveForm.ActiveControl.SelText = Clipboard.GetText()
Case 3 ' Delete.
' Delete selected text.
ActiveForm.ActiveControl.SelText = ""
End Select
End If
End Sub