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

Article translations Article translations
Article ID: 943328 - View products that this article applies to.
Expand all | Collapse all

On This Page

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.

MORE INFORMATION

Steps to reproduce the problem

  1. Start Microsoft Visual Studio 2005.
  2. Create a Microsoft Visual C# project. Name the project "CloseEventTest."
  3. 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
    • Show the MDI child form again
  4. Run the following code:
    Form2 f = null;
    
    private void button1_Click(object sender, EventArgs e) {
        if (f != null) {
            MessageBox.Show("Form already created");
            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("Form already created");
            return;
        }
        f = new Form2();
        f.MdiParent = this;
        f.Show();
    
    }
    
    private void button4_Click(object sender, EventArgs e) {
        if (f != null)
            f.Show();
    
    }
  5. Create a new form. Name the form "form2."
  6. Add trace code in the following events:
    • Closing
    • FormClosing
    • Closed
    • FormClosed
  7. 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("OnHandleDestroyed");
        base.OnHandleDestroyed(e);
    }
    
    protected override void OnClosed(EventArgs e) {
        MessageBox.Show ("OnClosed");
        base.OnClosed(e);
    }
    
    protected override void OnClosing(CancelEventArgs e) {
       MessageBox.Show ("OnClosing");
        base.OnClosing(e);
    }
    
    protected override void OnFormClosed(FormClosedEventArgs e) {
        MessageBox.Show ("OnFormClosed"); 
        base.OnFormClosed(e);
    }
    
    protected override void OnFormClosing(FormClosingEventArgs e) {
        MessageBox.Show ("OnFormClosing");
        base.OnFormClosing(e);
    }
    
  8. Close the hidden MDI child form.
Notice that the events do not expire as expected.

Properties

Article ID: 943328 - Last Review: December 3, 2007 - Revision: 2.0
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
Keywords: 
kbexpertiseadvanced kbtshoot kbprb KB943328

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com