Microsoft KB Archive/943328

= Some events do not expire in the correct order when you close a hidden MDI child form in a Windows Form application =

Article ID: 943328

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft Visual Studio 2005 Team Suite
 * Microsoft Visual Studio 2005 Team System Test Edition
 * Microsoft Visual Studio 2005 Team Edition for Database Professionals
 * Microsoft Visual Studio 2005 Team Edition for Software Architects
 * Microsoft Visual Studio 2005 Team Edition for Software Developers
 * Microsoft Visual Studio 2005 Team Edition for Software Testers
 * Microsoft Visual Studio 2005 Standard Edition
 * Microsoft Visual Studio 2005 Professional Edition
 * Microsoft Visual Studio 2005 Express Edition

-



SYMPTOMS
When you close a form in a Windows Form application, the following events expire in the order in which they appear in the following list:
 * 1) Closing
 * 2) FormClosing
 * 3) Closed
 * 4) FormClosed

However, when you close a hidden multiple document interface (MDI) child form, these events do not expire in the correct order.

Additionally, if the hidden MDI child form contains an ActiveX control, the MDI child form is destroyed in memory during the lifetime of the ActiveX control instance. Therefore, the MDI child form is closed but the control is not closed.



CAUSE
For MDI child forms, the application destroys the handler when the MDI form is hidden. This operation differs from the operation of a non-MDI child form. For non-MDI child forms, the application preserves the handler until the form is disposed. Therefore, the events of a non-MDI child form are raised even when the form is hidden.



WORKAROUND
To work around this problem, follow these steps:
 * 1) Detach the hidden MDI child form from the application.
 * 2) Perform the operations in the detached hidden MDI child form.
 * 3) Reattach the hidden MDI child form to the application.



Steps to reproduce the problem
 Start Microsoft Visual Studio 2005. Create a Microsoft Visual C# project. Name the project &quot;CloseEventTest.&quot; Create four button controls. Each button control is used to perform one of the following tasks:  Create a modeless form Create an MDI child form Close the MDI child form</li> Show the MDI child form again</li></ul> </li>  Run the following code: Form2 f = null;

private void button1_Click(object sender, EventArgs e) { if (f != null) { MessageBox.Show(&quot;Form already created&quot;); return; }   f = new Form2; f.Show; }

private void button2_Click(object sender, EventArgs e) { if (f != null) { f.Close; f = null; } }

private void button3_Click(object sender, EventArgs e) { if (f != null) { MessageBox.Show(&quot;Form already created&quot;); return; }   f = new Form2; f.MdiParent = this; f.Show;

}

private void button4_Click(object sender, EventArgs e) { if (f != null) f.Show;

} </li> Create a new form. Name the form &quot;form2.&quot;</li> Add trace code in the following events: <ul> Closing</li> FormClosing</li> Closed</li> FormClosed</li></ul> </li>  Use the following code to hide the MDI child form: private void HideButton_Click(object sender, EventArgs e) { this.Hide; }

protected override void OnHandleDestroyed(EventArgs e) { MessageBox.Show(&quot;OnHandleDestroyed&quot;); base.OnHandleDestroyed(e); }

protected override void OnClosed(EventArgs e) { MessageBox.Show (&quot;OnClosed&quot;); base.OnClosed(e); }

protected override void OnClosing(CancelEventArgs e) { MessageBox.Show (&quot;OnClosing&quot;); base.OnClosing(e); }

protected override void OnFormClosed(FormClosedEventArgs e) { MessageBox.Show (&quot;OnFormClosed&quot;); base.OnFormClosed(e); }

protected override void OnFormClosing(FormClosingEventArgs e) { MessageBox.Show (&quot;OnFormClosing&quot;); base.OnFormClosing(e); } </li> Close the hidden MDI child form.</li></ol>

Notice that the events do not expire as expected.

Keywords: kbexpertiseadvanced kbtshoot kbprb KB943328

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.