---
title: "Closing multiple windows form with single click"  
description: "Closing multiple windows form with single click"  
author: "Anonymous User"  
published: 2014-08-14  
updated: 2014-08-14  
canonical: https://www.mindstick.com/forum/2030/closing-multiple-windows-form-with-single-click  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Closing multiple windows form with single click

I want to [close](https://yourviews.mindstick.com/story/1687/rubbing-hands-close-up-benefits) two [forms](https://www.mindstick.com/interview/34192/what-is-the-purpose-of-the-authentication-mode-forms-element-in-web-config) at the same time. I have one main [form](https://www.mindstick.com/forum/6/multi-form-mfc-application) that starts with program. when [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) [click button](https://www.mindstick.com/forum/34274/how-to-pass-combobox-value-from-windows-form-edirrow-to-windows-form-form1-where-click-button), the main form will hide and other form will [pop up](https://answers.mindstick.com/qa/50233/what-to-do-with-the-issues-that-may-pop-up-at-the-time-of-usb-device-installation). on the [second](https://answers.mindstick.com/qa/41761/how-did-the-second-industrial-revolution-influence-women-s-roles-in-society) form if user click "back to main " button, it should hide second form and show main form. But the [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) is if user tries to close the second form it should close the main form as well. How can i close the main form as well

## Replies

### Reply by Pravesh Singh

Hi Ankit, \
You can try this:

public partial class MainForm : Form

{

//"[Click](https://www.mindstick.com/articles/12423/social-login-magento-2-one-click-to-register-social)" event of the [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net) that should opens the second form:

private void goToSecondForm_Click(object sender, EventArgs e)

{

Form2 f2 = new Form2(); //Or you can write it out of this method.

this.Hide(); //Hides the main form.

f2.ShowDialog(); // Shows the second form.

this.Show(); // Shows the main form again, after closing the second form using your own button.

}

}

public partial class Form2 : Form

{

bool selfClose = false; //False shows that user closed the second form by default button and true shows that user closed it by your own button.

//"Click" event of the button that should closes just the second form and returns user to the main form:

private void ownCloseButton_Click(object sender, EventArgs e)

{

selfClose = true; //Means user clicked on your own button.

this.Close(); //So the program closes the second form and runs f2_FormClosed method, but because selfClose became true here, happened nothing there and program will go back to goToSecondForm_Click method in the main form and will run this.Show() .

}

//"FormClosed" event of the second form :

//Whether user clicked on your own button or on the default one, this method will run.

private void f2_FormClosed(object sender, FormClosedEventArgs e)

{

if (!selfClose) //It means user didn't click on your own button and both of forms must be closed .

Application.Exit(); //So the program closes all of forms (actually closes the program) and couldn't access to any other commands (including this.Show() in goToSecondForm_Click method).

}

}


---

Original Source: https://www.mindstick.com/forum/2030/closing-multiple-windows-form-with-single-click

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
