How to Close Parent Form from Child Form in Windows Forms 2.0

Posted by: Suprotim Agarwal , on 3/13/2008, in Category WinForms & WinRT
Views: 89656
Abstract: With the MSDN forums flooded with similar questions, I decided to dedicate an article for the subject. In this article, we will create two forms, a parent and a child and then open the child form using the Parent Form. When the child form closes, we will close the Parent form too.
How to Close Parent Form from Child Form in Windows Forms 2.0
 
With the MSDN forums flooded with similar questions, I decided to dedicate an article for the subject. In this article, we will create two forms, a parent and a child and then open the child form using the Parent Form. When the child form closes, we will close the Parent form too. Let us see how.
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 new 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:
 
C#
        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();
        }
VB.NET
      Private Sub btnOpenForm_Click(ByVal sender As Object, ByVal e As EventArgs)
                  Dim frm2 As Form2 = New Form2()
                  AddHandler frm2.FormClosed, AddressOf frm2_FormClosed
                  frm2.Show()
                  Me.Hide()
      End Sub
 
      Private Sub frm2_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
                  Me.Close()
      End Sub
The trick in this code is to create the FormClosedEventHandler delegate which represents the method that handles a FormClosed event, in our case, for Form2. We then subscribe to this event. So whenever Form2 closes, we close the parent form Form1 too.
That’s it. That was simple. Thanks to a guy called Anderj who shared this idea.
I hope you liked the article and I thank you for viewing it.
If you liked the article, please subscribe to the RSS feed over here.
 
 
 
Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Suprotim Agarwal, ASP.NET Architecture MVP, MCSD, MCAD, MCDBA, MCSE, is the CEO of A2Z Knowledge Visuals Pvt. He primarily works as an Architect Consultant and provides consultancy on how to design and develop .NET centric database solutions.

Suprotim is the founder and primary contributor to DotNetCurry, DNC .NET Magazine, SQLServerCurry and DevCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls. and is authoring another one at The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Donnex Kujaliwa on Wednesday, June 25, 2008 5:00 AM
I am develop an internet cafe billing system am a little bit confused on how to connect the two apllications Serverside and the Clientside which is going to dislay Time and cost to the customer may you assist.
King regards
Comment posted by Prashant on Thursday, October 2, 2008 6:27 AM
thanx buddyyyyyy.
Comment posted by nanthakumar.t on Thursday, March 5, 2009 3:11 AM
this is article very useful to me...

Thanks...
Comment posted by Aarti on Friday, February 5, 2010 7:53 AM
want to close current form and open new form in asp.net
How can i do
it
i am doing project in asp.net i.e web application

plz help me
Comment posted by dp on Friday, August 6, 2010 2:14 AM
It doesn't works in VS 2008 and 2010. on the line frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed); it says "The name 'frm2_FormClosed' does not exist in the current context"

Any Suggestions ?
Comment posted by Brite on Wednesday, March 16, 2011 1:59 AM
No need for all this. In the program.cs change the code like this
Default will be Application.Run(Form1());

change it to

Form1 form = new Form1();
form.Show();
Application.Run();

Now you can dispose any form and show any form without closing the application.
Also to exit the application when all forms are closed you need to add this code to the formclosing event of all forms.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Dispose();
            Application.Exit();
        }
Comment posted by Haresh Paghdar on Monday, March 21, 2011 11:10 AM
It is Nice Artical for when We started software to make Desktop Application
Comment posted by AC on Friday, October 14, 2011 7:20 AM
Hello,
How to read the parent form datagrid into child form.
thanks
Comment posted by srinivasan sampath on Tuesday, February 7, 2012 3:27 AM
i want website link for learning window application using c#
Comment posted by Mohammad Shahnawaz on Wednesday, June 13, 2012 6:50 AM
// Suppose you have two Form
// First Form Name is Form1
// Second Form Name is Form2
// You have to jump from Form1 to Form2 enter code here
// Write code like following
// On Form1 i have one button named Button1, and its click option write the program..

protected void Button1_Click(Object sender,EventArgs e)
{
              Form frm=new Form2();// I have created object of Form2
                frm.Show();
               this.Visible=false;
               this.Hide();
               this.Close();
               this.Dispose();
}
Comment posted by nnnnnnnnnnnn on Thursday, July 26, 2012 1:38 AM
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
Comment posted by سنیا جهرمی on Thursday, August 8, 2013 5:09 AM
WOW!
thanks you helped me a lot!!!!
i even couldn't find an answer such beautiful ,fast & clear like this in stack overflow!
thanks U VERYYY MUCH!
Comment posted by .Nexter on Tuesday, July 29, 2014 4:17 PM
Thank you this has helped me a lot!!

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel