How can we close the form with the x in the upper corner of the windows Form? When I press x, it exits the form but the program is still running in the background.
Loading
How can we close the form with the x in the upper corner of the windows Form? When I press x, it exits the form but the program is still running in the background.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 20, 2023, 12:00 AM EDT
Hello Mehmet,
To add a confirmation message with the "Yes" and "No" buttons to the FormClosing event handler, you can use the MessageBox.Show method with the MessageBoxButtons.YesNo option. Here is an example implementation:
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { // Check if the form is being closed by the user clicking the X button if (e.CloseReason == CloseReason.UserClosing) { // Display a confirmation message DialogResult result = MessageBox.Show("Are you sure you want to exit?", "Confirmation", MessageBoxButtons.YesNo); if (result == DialogResult.No) { // Cancel the form closing event e.Cancel = true; } else { // Terminate the application Application.Exit(); } } }In this implementation, if the user clicks the "No" button in the confirmation message, the form closing event is canceled and the application continues running. If the user clicks the "Yes" button, the application is terminated.
Thanks
Naimish Makwana
Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 19, 2023, 12:52 PM EDT
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { // Check if the form is being closed by the user clicking the X button if (e.CloseReason == CloseReason.UserClosing) { // Terminate the application Application.Exit(); } }Tuhin PaulRank #7 · 71.9K reputationPosted Apr 19, 2023, 1:29 PM EDT
When you press the x in the upper corner of a Windows Form, it typically triggers the FormClosing event, which you can handle in your code to perform any necessary cleanup actions before the form is closed. The form is simply hidden rather than closed, which can result in your program still running in the background. To ensure that your program completely exits when the user clicks the x, you can handle the FormClosing event and set the e.Cancel property to false.
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { // Perform any necessary cleanup actions here // Set e.Cancel to false to allow the form to close e.Cancel = false; }Tuhin PaulRank #7 · 71.9K reputationPosted Apr 19, 2023, 1:30 PM EDT
By setting
e.Canceltofalse, you indicate that it is okay to close the form and that the program should exit. If you do not sete.Canceltofalse, the form will be hidden but the program will continue running in the background.Mehmet FatihRank #1,474 · 1.1K reputationPosted Apr 19, 2023, 1:40 PM EDT
Thanks a lot Rajkiran Swain. This code works fine but how can we add confirm message for closing with the MessageBoxButtons.YesNo?
Mehmet FatihRank #1,474 · 1.1K reputationPosted Apr 19, 2023, 2:18 PM EDT
Thanks everybody. The problem has been solved with the code below.
if (e.CloseReason == CloseReason.UserClosing) { if (MessageBox.Show("Are you Sure you want to Exit. Click Yes to Confirm and No to continue", "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { e.Cancel = true; } else { e.Cancel = false; Application.Exit(); } }Sam HobbsRank #107 · 20.5K reputationPosted Apr 19, 2023, 3:48 PM EDT
It depends on why the program does not exit when the x does not cause it too. You do not show the code so we dont know what is causing that. A good solution might be to remove some code, not add more.