Reputation: 90
MSDN documentation states:
When a form is owned by another form, it is closed or hidden with the owner form. For example, consider a form named
Form2
that is owned by a form namedForm1
. IfForm1
is closed or minimized,Form2
is also closed or hidden.
Apparently the hiding isn't working recursively? When I have a stack of 4 forms who are parented to each other (GrandChildForm.Owner = Child; ChildForm.Owner = ParentForm;
etc.), minimizing any one of them only minimizes it's direct child too.
Similar effect when closing one of these forms, only the FormClosing/Closed events of the direct child are raised, but not for the other accestors. Again the docs don't state that this doesn't work recursively:
If a form has any child or owned forms, a
FormClosing
event is also raised for each one. If any one of the forms cancels the event, none of the forms are closed.
What I'm trying to achieve:
FormClosingEventArgs.Cancel = true;
) then the deal is off.Should this be implemented using extra event handling (subscribing to the Owner's FormClosing
/FormClosed
/SizeChanged
events) or am I missing something here?
Upvotes: 2
Views: 3055
Reputation: 1761
You can inherit your form from this class:
public class AdvancedForm : Form
{
protected override void OnFormClosing(FormClosingEventArgs e)
{
foreach (Form f in this.OwnedForms)
{
f.Close();
}
base.OnFormClosing(e);
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
foreach (AdvancedForm f in this.OwnedForms)
{
switch (this.WindowState)
{
case FormWindowState.Minimized:
case FormWindowState.Normal:
f.WindowState = this.WindowState;
break;
case FormWindowState.Maximized:
// just restore owned forms to their original sizes when parent form is maximized
f.WindowState = FormWindowState.Normal;
break;
}
// OnSizeChanged must be called, as changing WindowState property
// does not raise SizeChanged event
f.OnSizeChanged(EventArgs.Empty);
}
}
}
Or just use code from this class in "Closing" and "SizeChanged" event handlers.
Upvotes: 3