I am working on a windows form application. I want to show user a message stating close reason when user clicks "X" button in the main window.With "X" button i mean "close" button in "minimize","maximize" and "close" tray in windows.
I have written this code.
private void frmIMS_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("This application is closing down because of " + e.CloseReason.ToString() + ". Do you really want to close it ?", "", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
else
{
Application.Exit();
}
}
Now what happens is,If user clicks no to message box,event is discarded and when user clicks yes,form_closing() fires again and shows other messagebox.So messagebox is shown twice.I want to show it once.Please help and tell why is it firing twice.