Reputation: 7148
I have started prototyping a C# MDI application and am running into an issue. It seems that when an MDIChild is open in the MDIParent I have to hit the close buttom on the parent multiple times to close the application. Each click on the close button closes one of the MDIChildren.
I suspected that it had to do with my MDIChildren's base form's on close method.
private void _AssetFormBase_FormClosing(object sender, FormClosingEventArgs e)
{
if(sender != this.MdiParent)
{
e.Cancel = true;
this.Hide();
}
}
Though my trick above does not seem to work. I assume that when the MDIParents close is called it in turn first calls all of its childrens close methods. So if the sender is the parents then instead of cancelling and hiding (to preserve the forms state), I would not do this and allow whatever normally happens to happen.
Any idea what the issue might be?
Upvotes: 0
Views: 129
Reputation: 942050
The sender is not what you think it is. Use e.CloseReason instead, you'll get CloseReason.MdiFormClosing. But don't test for that specific value, you also don't want to prevent the operating system from shutting down. Use:
private void _AssetFormBase_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.Hide();
}
}
Note that you'll also get UserClosing when you call Close() in your own code.
Upvotes: 2