Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I close a parent Form from child form in Windows Forms 2.0?

Tags:

c#

winforms

I have a need to close a parent form from within child form from a Windows application. What would be the best way to do this?

like image 909
Jason Von Ruden Avatar asked Mar 01 '23 08:03

Jason Von Ruden


1 Answers

I ran across this blog entry that looks like it will work and it uses the Event Handler concept from D2VIANT Answer

http://www.dotnetcurry.com/ShowArticle.aspx?ID=125

Summary: Step 1: Create a new Windows application. Open Visual Studio 2005 or 2008. Go to File > New > Project > Choose Visual Basic or Visual C# in the ‘Project Types’ > Windows Application. Give the project a name and location > OK.

Step 2: Add a n ew form to the project. Right click the project > Add > Windows Forms > Form2.cs > Add.

Step 3: Now in the Form1, drag and drop a button ‘btnOpenForm’ and double click it to generate an event handler. Write the following code in it. Also add the frm2_FormClosed event handler as shown below:

    private void btnOpenForm_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
        frm2.Show();
        this.Hide();
    }


    private void frm2_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Close();
    }
like image 55
Jason Von Ruden Avatar answered May 08 '23 08:05

Jason Von Ruden