MDI Form detecting with a child form is added or removed

前端 未结 8 782
心在旅途
心在旅途 2020-12-21 08:11

Is there an event I can use to tell if a child form has been added or removed from the MDI parent?

8条回答
  •  Happy的楠姐
    2020-12-21 08:20

    Since the MdiChildActivate event is fired before the MDI child form is actually closed and therefore there isn't enough information to detect if a MDI child form is actually activated or closed, I chose a different approach to solve the problem.

    I found that the ParentChanged event fires on the MDI child form when it is closed.

    public class MdiParentForm : Form
    {
        // ...
    
        private Form CreateMdiChildForm()
        {
            var form = new MdiChildForm
            form.ParentChanged += MdiFormParentChangedHandler;
            form.MdiParent = this;
            return form;
        }
    
        private void MdiFormParentChangedHandler(object sender, EventArgs args)
        {
            var form = sender as Form;
            if (form != null)
            {
                if (form.MdiParent != null)
                {
                    // MDI child form will activate
                    // ... your code here
                }
                else
                {
                    // MDI child form will close
                    form.ParentChanged -= MdiFormParentChangedHandler;
                    // ... your code here
                }
            }
        }
    
        // ...
    }
    

提交回复
热议问题