none
How to close only mdi child forms in windows c# RRS feed

  • Question

  • Hi all,

    Hope doing well,

    sir i am making one windows application project where i used one mdi parent in that forms i used some menu when i click on particular it's opening one form in that form i used exit button.

    when i click on exit button in that button event i used this code: this.close();

    but when i click on exit button it is closing whole application. i want to close only currently opened child form.

    How to do this?

    Thanks


    vicky Bangalore

    Thursday, February 20, 2014 2:20 PM

All replies

  • you can try the following code

    foreach (Form frm in Application.OpenForms)
                {
                    if (frm.MdiParent == this)
                    {
                        frm.Dispose();
                    }
                }

     however i have not tested..

    thanks


    Hirendra Sisodiya

    Thursday, February 20, 2014 2:50 PM
  •             List<Form> formList = new List<Form>();
                foreach (Form item in Application.OpenForms)
                {
                    if (item.IsMdiChild)
                        formList.Add(item);
                }
    
                while (formList.Count > 0)
                {
                    formList.ForEach(delegate(Form form)
                    {
                        form.Close();
                        formList.Remove(form);
                    });
                }
    

    This one works for me.

    Regards,

    Friday, May 29, 2015 1:48 PM