C# Application Form Closing Event Question

|

Sometimes you have a program that you would prefer someone not close without confirming they truly meant to close it.

This script will assist in providing a question before the program simply closes.

private void fm1_FormClosing(object sender, FormClosingEventArgs e)
		{
			DialogResult dialogResult = MessageBox.Show("Are you sure you want to close?", Application.ProductName, MessageBoxButtons.YesNo);
			if (dialogResult == DialogResult.Yes)
			{
				// Truly Close
				e.Cancel = false;
			}
			else if (dialogResult == DialogResult.No)
			{
				// Oops Don't close
				e.Cancel = true;
			}
		}
Originally Posted on March 23, 2014
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.