(C#) Confirmation when closing a form

By | August 31, 2012

Add an event handler for Form Closing and use this code. This is a really simple solution.

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
  if (MessageBox.Show("Are you sure?", "Close application", MessageBoxButtons.YesNo) == DialogResult.Yes)
     e.Cancel = false;
  else
     e.Cancel = true;
} 

Leave a Reply

Your email address will not be published.