Double confirmation on exit

I'm trying to make the user be asked to confirm the exit from my program in C #, but for some reason, if they say yes, they would like to exit, the confirmation window will appear again. I can’t understand why.

if (MessageBox.Show("Are you sure you want to exit?", "Confirm exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { e.Cancel = true; } else { Application.Exit(); } 
+6
c # winforms exit confirm
source share
3 answers

Did you check CloseReason for the FormClosing event? I think that you can get the same event for two reasons (although I definitely do not expect this to happen normally); check out FormClosingEventArgs to see what the parameters are.

+4
source share

Use this

  private void frmMain_FormClosing(object sender, FormClosingEventArgs e) { if (MessageBox.Show("Are you sure you want to close?", "Infomate", MessageBoxButtons.YesNo) == DialogResult.No) { e.Cancel = true; } } 
+7
source share

Ah, I figured out how to fix it. I deleted Application.Exit (); event from the FormClosing event, and moved it to the FormClosed event. Now everything works.

+3
source share

All Articles