1

I have a mdi parent form and I open my other forms in run time as mdi child form by this code:

private void winAppToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Upload objWA = new Upload();
            objWA.MdiParent = this;
            objWA.Show();
            //objWA.WindowState = FormWindowState.Maximized;
        }

        private void userInfoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Reports objUI = new Reports();
            objUI.MdiParent = this;
            objUI.Show();
            //objUI.WindowState = FormWindowState.Maximized;
        }

but the problem is: When current form is open, user can open another form and it can be repeated several times so that each form is opened what's code for closing the previous child form before user open a new child form??

Screen shot for reference if we see the image my upload and reports forms both are getting opened on one another but it should only show the currently opened form

kunal
  • 11
  • 1
  • 8
  • 1
    Possible duplicate of [Close all open forms except the main menu in C#](https://stackoverflow.com/questions/9029351/close-all-open-forms-except-the-main-menu-in-c-sharp) – boop_the_snoot Sep 29 '17 at 07:14
  • i had already tried this but its not working – kunal Sep 29 '17 at 07:16
  • Do u want to close previous instances of the _form_, when a new instance is created? If that is so, then any unsaved data in the previous one would get lost – boop_the_snoot Sep 29 '17 at 07:17
  • yes i should close or hide previous form when new form is clicked – kunal Sep 29 '17 at 07:37

2 Answers2

0
public void DisposeAllButThis(Form form){
foreach (Form frm in this.MdiChildren)
{
    if (frm.GetType() == form.GetType() 
        && frm != form)
    {
        frm.Dispose();
        frm.Close();
    }
}}

got this from: C# loop through all MDI childer and close all other forms except the current

timo stevens
  • 372
  • 1
  • 3
  • 15
  • could you please edit my post and can you show where should i include your code because i am completely new to this – kunal Sep 29 '17 at 07:21
0
private void winAppToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Upload objWA = new Upload();
        objWA.MdiParent = this;
        objWA.Show();
        //objWA.WindowState = FormWindowState.Maximized;
    }

    private void userInfoToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Reports objUI = new Reports();
        objUI.MdiParent = this;
        objUI.Show();
        DisposeAllButThis(Form objUI);
        //objUI.WindowState = FormWindowState.Maximized;
    }
    public void DisposeAllButThis(Form form)
    {
        foreach (Form frm in this.MdiChildren)
        {
            if (frm.GetType() == form.GetType() 
                && frm != form)
            {
                frm.Dispose();
                frm.Close();
            }
        }
    }

let me know if it worked because i just made it up

timo stevens
  • 372
  • 1
  • 3
  • 15
  • i am getting three errors near DisposeAllButThis(Form objUI); Error1 ) expected Error 2 Invalid expression term ')' Error 3 ; expected – kunal Sep 29 '17 at 07:43