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.
class MyForm {
protected override void OnFormClosing(FormClosingEventArgs e) {
if (e.CloseReason != CloseReason.UserClosing) return;
}
}
—SA