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: 160095
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:
        private void btnOpenForm_Click(object sender, EventArgs e)
            Form2 frm2 = new Form2();
            frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
        private void frm2_FormClosed(object sender, FormClosedEventArgs e)
      Private Sub btnOpenForm_Click(ByVal sender As Object, ByVal e As EventArgs)
                  Dim frm2 As Form2 = New Form2()
                  AddHandler frm2.FormClosed, AddressOf frm2_FormClosed
      End Sub
      Private Sub frm2_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
      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.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for ten consecutive times. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
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...

Comment posted by Aarti on Friday, February 5, 2010 7:53 AM
want to close current form and open new form in
How can i do
i am doing project in 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();

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)
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
How to read the parent form datagrid into child form.
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
Comment posted by nnnnnnnnnnnn on Thursday, July 26, 2012 1:38 AM
Comment posted by سنیا جهرمی on Thursday, August 8, 2013 5:09 AM
thanks you helped me a lot!!!!
i even couldn't find an answer such beautiful ,fast & clear like this in stack overflow!
Comment posted by .Nexter on Tuesday, July 29, 2014 4:17 PM
Thank you this has helped me a lot!!
Comment posted by ddd on Friday, January 9, 2015 1:26 AM