Reputation: 9876
I know that there are examples of the similar problem even here in StackOverflow but I have difficulties to understand the nature of the problem and thus - to solve my custom case.
What I have right now is this code:
bool closingPending = false;
private void MyFormN_FormClosing(object sender, FormClosingEventArgs e)
{
if (closingPending) return;
//DialogResult answer = MessageBox.Show("Do you want to save changes ?", "Save",
if (MessageBox.Show("Do you want to save changes ?", "Save",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button3) == DialogResult.Yes)
{
closingPending = true;
MessageBox.Show("To Do - validate and save");
}
if (MessageBox.Show("Do you want to save changes ?", "Save",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button3) == DialogResult.Cancel)
{
closingPending = true;
e.Cancel = true;
}
if (MessageBox.Show("Do you want to save changes ?", "Save",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button3) == DialogResult.No)
{
closingPending = true;
Application.Exit();
}
}
It's a result from my attempts and some ideas I got from other posts here. But what happens when I'm executing this code is - by pressing the [x]
of the window i get the messagebox shown but no matter which button I click the form is shown several times. Before adding closingPending
I'm pretty sure that this problem occurred only when I was trying Application.Exit()
. The closingPending
exmple worked when I try this example:
if(closingPending) return;
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
{
closingPending = true;
Application.Exit();
}
However I got some idea on what cause the problem but I'm still far away from understanding it completely or solving it.
Upvotes: 0
Views: 850
Reputation: 9126
try something like below... it will solve your problem....
DialogResult result = MessageBox.Show("Do you want to save changes ?", "Save",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button3);
switch (result)
{
case DialogResult.Yes:
closingPending = true;
MessageBox.Show("To Do - validate and save");
break;
case DialogResult.No:
closingPending = true;
Application.Exit();
break;
case DialogResult.Cancel:
closingPending = true;
e.Cancel = true;
break;
}
Upvotes: 2