Microsoft KB Archive/190634

= PRB: Activate Event Is Not Triggered with Child Form of MDI Form =

Article ID: 190634

Article Last Modified on 1/8/2003

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q190634



SYMPTOMS
The Activate event for an MDIChild form is only triggered the first time the form is shown. For example, when an MDIChild form is displayed using the Show Method, then hidden using the Hide method, and then shown again using the Show method, the Activate event is triggered only the first time the form is shown.



CAUSE
The Activate and Deactivate events occur for MDI child forms only when the focus changes from one child form to another. In an MDIForm object with two child forms, the child forms receive these events when the focus moves between them. However, when the focus changes between a child form and a non-MDI child form, the parent MDIForm receives the Activate and Deactivate events.



RESOLUTION
To work around this behavior, create a custom method in Form1 to handle showing the form. In the custom method, place code to show the form as well as code you would have placed in the Activate event. To display the child form, call the custom method instead of using the form's Show method. An example custom method to show a child form and simulate an Activate event would be:

Public Sub MyShow Form1.Show MsgBox "MyShow - simulation of Activate event" End Sub

The call to invoke this custom method would be:

Form1.MyShow 'Call the custom method created in Form1



STATUS
This behavior is by design.



Steps to Reproduce Behavior
 Create a new standard EXE project. Form1 is created by default. Choose Project, Add MDI Form, to add an MDI form. Choose Project, Project1 Properties, and then set MDIForm1 to be Project1's Startup Object. Select Form1 and change the Name and Caption properties of Form1 to frmChild. Set frmChild's MDIChild property to True.  In the Activate event of frmChild, place the following code: MsgBox "MDIChild Activate Event"

</li>  Place a CommandButton on frmChild. In the Click event for the CommandButton, place the following code: Me.Hide

</li>  Select MDIForm1 (the parent form). Open the Visual Basic Menu Editor to place a menu item on the MDI form. Set the Caption of the menu item to "Show MDIChild." Set the Name of the menu item to "mnuShow." In the Click event for the mnuShow menu item, place the following code: rmChild.Show

</li> Press the F5 key to run the application. Click the Show MDIChild menu item. The message box appears as the child form is displayed. Click the CommandButton on frmChild to hide the child. Click the Show MDIChild menu item again. The message box does not appear, indicating that the MDIchild's Activate event did not fire.</li>  Select frmChild. Open the form's code window and place the following workaround code: Public Sub MyShow Me.Show MsgBox "MyShow acting as Activate event" End Sub

</li>  Select MDIForm1. In the Click event for the mnuShow menu item, replace the previous code with: frmChild.MyShow

</li> Press the F5 key to run the application. Click the Show MDIChild menu item. The message box appears as the child form is displayed. Click the CommandButton on frmChild to hide the child. Click the Show MDIChild menu item again. The message box appears again.</li></ol>

<div class="references_section">