Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my application i have one parent form and two child forms for that parent form here my parent form is Form1 and
my child forms are Upload and Reports on clicking upload i need to close reports form and if i click reports i need to close
upload form how can i do this below is my code

<pre lang="c#">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;
        }


What I have tried:

how can i colse the previous child form if i had opened another child form
Posted
Updated 28-Sep-17 21:42pm

1 solution

The first thing to do is to note that you want to keep references to the two forms: move the objWA and objUI (which are both badly named - "obj" is a very bad prefix, even if pseudo-Hungarian notation was recommended for C#, which it isn't) outside of the methods and into the MDI parent form class as private variables.
Initialize them to null, check for null when you handle you click events and issue the appropriate close instructions from there. You will also need to handle the form close event:
C#
private Upload formUpload = null;
private Reports formReport = null;
...
private void winAppToolStripMenuItem_Click(object sender, EventArgs e)
    {
    if (formUpLoad != null) return;
    formUpload = new Upload();
    formUpload.MdiParent = this;
    formUpload.FormClosed += formUploadClosed;
    if (formReport != null) formReport.Close();
    formUpload.Show();
    }
private void formUploadClosed(object sender, EventArgs e)
    {
    formUpload = null;
    }
Repeat that for the other from, and you should be good to go.
 
Share this answer
 
Comments
Member 12324523 29-Sep-17 4:11am    
but by default it should show my parent form and if i click upload it should show upload form and if i click reports it should show reports form but by default after running it is showing upload form
OriginalGriff 29-Sep-17 4:16am    
Then somewhere in you app you are creating a form instance: search for all references to the Upload class and see what you get.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900