The problem is that the messagebox with "sure you wanna close?" does pop up, but when I click "no", it still proceeds to close the program. Any suggestions? Here's my code:
protected override void OnFormClosing(FormClosingEventArgs e)
{
CloseCancel();
}
public static void CloseCancel()
{
const string message = "Are you sure that you would like to cancel the installer?";
const string caption = "Cancel Installer";
var result = MessageBox.Show(message, caption,
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
Environment.Exit(0);
}
just take it simple
e.Cancel on the FormClosing event will stop the closing process
I'm unsure of how to handle the other scenario you mentioned (handling the 'X' click). Maybe you could do something like this (psuedo-code):
The question is now old but this way is more simple and short, and I think it can be useful to those who arrive on this page:
and use elsewhere
this.Close()
rather than a function.You are expected to set the
Cancel
property of theFormClosingEventArgs
argument totrue
when you require the close-operation to be cancelled. And an explicitEnvironment.Exit(0)
is normally not required since the form is on its way to being closed any way (the cancellation of the shutdown process is opt-in, not opt-out).Replace the last bit with: