Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1180
Calling
posted

I create MDI children and load them into application like this:

frm.MdiParent = this; frm.Show();

When tab is closing MdiChildActivate is getting called on MDI parent.

ActiveMdiChild is null (which is correct, last tab was just closed),

but this.MdiChildren.Last() return form that is closing as if it was still visible.

Can I somehow detect that form is being closed inside mdichildactivate?

  • 6158
    Suggested Answer
    Offline posted

    Hello,

    The simplest way to determine if the form is being closed is to use reflection. The .NET Form class has an internal IsClosing property that indicates if a form is in process of being closed.

            private void Form1_MdiChildActivate(object sender, EventArgs e)

            {

                if (this.ActiveMdiChild == null)

                {

                    Form lastForm = this.MdiChildren.Last();

                    Type type = typeof(Form);

                    PropertyInfo propertyInfo = type.GetProperty("IsClosing", BindingFlags.Instance | BindingFlags.NonPublic);

                    bool isClosing = (bool)propertyInfo.GetValue(lastForm, null);

                }

            }

     

    If you want to avoid using reflection, you can always subscribe the FormClosing event on the child forms, and keep track of which form is being closed and check that against the form in the MdiChildActivate event handler.

    I hope this helps.

    Chris