Bart Friederichs
Bart Friederichs

Reputation: 33511

MDI Parent's FormClosing event always closes one MDI child

I have an MDI parent with this FormClosing event handler:

private void FrmMdiParent_FormClosing(object sender, FormClosingEventArgs e) {
    e.Cancel = true;
}

and when I click the red cross on the window when I have some MDI children present, it will close exactly one MDI child. When I remove e.Cancel = true, the behaviour is the same, except it will close the parent form when all children are gone.

The children have no FormClosing handler registered.

How do I cancel the FormClosing event without closing any MDI children?

Upvotes: 2

Views: 1441

Answers (1)

King King
King King

Reputation: 63327

foreach(Form f in yourMDIForm.MdiChildren)
    f.FormClosing += ChildFormClosing;
private void ChildFormClosing(object sender, FormClosingEventArgs e){
    if(e.CloseReason == CloseReason.MdiFormClosing) e.Cancel = true;
}

Upvotes: 2

Related Questions