Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
DialogResult dialogResult = MessageBox.Show("Do you realy want close the application?", "Exit",
                MessageBoxButtons.YesNo);

            if (dialogResult==DialogResult.Yes)
            {
              Application.Exit();
            }
            else if (dialogResult==DialogResult.No)
            {
                e.Cancel = true;
            }
Posted

The problem is with the line
C#
Application.Exit();
You are already attempting to exit the application when you are in the Form_Closing event. All you need is
C#
DialogResult dialogResult = MessageBox.Show("Do you realy want close the application?", "Exit",
    MessageBoxButtons.YesNo);

if (dialogResult == DialogResult.No)
{
    e.Cancel = true;
}

If you put a breakpoint in the method and step through you will see that you re-enter the same event as soon as processing hits the line Application.Exit()
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Dec-15 11:13am    
Don't you think you should also check up CloseReason, to allow normal closing if the reason is different from UserClosing? I always do so. Please see Solution 3, where I also give another advice, on debugging.
—SA
BillWoodruff 2-Dec-15 11:18am    
+5
Don't use the Application.Exit call - all you need is the default e.Cancel = false and the closing of the main form will close the whole application automatically.

When you call Application.Exit, it tries to close the form again - so you get the query again when the Closing event is re-called.
 
Share this answer
 
Comments
BillWoodruff 2-Dec-15 11:18am    
+5
Some additional information to existing answers:

First of all, if something happens twice instead of once (a pretty typical problem), the first step to dig out the reason is using the debugger's "Call stack" window. Put a break point inside the called method, say, in the first like of the implementation, wait until it is called first and second time, in both cases, look at the call stack, to see where the call comes from. This is easy.

Also, when handling FormClosing (or, even better, overriding OnFormClosing) in such situations as yours, check up the reason:
https://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.closereason%28v=vs.110%29.aspx[^].

You only need to cancel closing of the reason is System.Windows.Forms.CloseReason.UserClosing. In all other cases, you don't need to do anything else, and should not cancel the event.

C#
class MyForm {
   // ...
   protected override void OnFormClosing(FormClosingEventArgs e) {
      if (e.CloseReason != CloseReason.UserClosing) return; // will be normally closed
      // now you can check up if something needs saving
      // and not show your dialog,
      // depending on result, set e.Cancel to true...
   }
}


—SA
 
Share this answer
 
Comments
BillWoodruff 2-Dec-15 11:18am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900