Search code examples
c#.netwinformsmdichildmdiparent

C# - Launch method in MdiParent when one specific MdiChild is closed


I'm actually building a program at school in .net. This is the first time I'm using it and I've got a question.

I got a MdiParent form which has two MdiChildren : a config form, and an articles list form. When you fill the config form, the articles list should appear.

I'm actually using this method in the MdiParent :

private void MotherMdi_MdiChildActivate(object sender, System.EventArgs e)
{
    ...;
}

But I need to deal with multiple variables because this method is fired every time a mdichild is open or closed ...

Is there a way to call a specific method only when One MdiChild is closed ?

Thanks a lot.


Solution

  • Event FormClosing is raised every time a windows form is closed. You can handle that event and do processing that you need to do when a form is closing.

    In the parent form:

    private void ParentForm_Load(object sender, EventArgs e)
            {
                ChildForm childForm = new ChildForm();
                childForm.childFormClosed += childForm_childFormClosed;
            }
    
            void childForm_childFormClosed(object sender, FormClosedEventArgs e)
            {
                // Handle processing here
            }
    

    In the child form:

    public event EventHandler<FormClosingEventArgs> childFormClosed;
    
            private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (childFormClosed != null)
                {
                    childFormClosed(sender, e);
                }
            }
    

    You may want to use FormClosed if that is what you need.

    You can also use your own inherited class if you want to pass more information than what FormClosingEventArgs would do.