views:

57

answers:

3

Is there a simple way of tracking the change of an Mdi's children i.e. when they are created and closed, something like an event OnMdiChildListChanged (I realise this doesn't actually exist).

I am also aware that I could have a method within my Mdi that handles the creation of child forms and logs the state of them or even create an Interface that defines that a child form has a "NotifyParent" method that is then called on close of the form, but i was wondering if there was any built in events that i could plumb into?

A: 

You could create your own event, and fire it in the associated methods yourself. It's fairly simple, and you can subscribe to it just like a built in event.

SLC
Hi @SLC, yeah i thought about that as well. I know there are a few ways to do it myself. I just thought that it might be something that .Net gives you as it seems that it would be fairly useful. Cheers
Ben
A: 

MdiChildActivate (or OnMdiChildActivate) will fire when a Mdi child is opened or closed.

riffnl
+1  A: 

This plumbing already exists, it is used to automatically update the MDI window list (MenuStrip.MdiWindowListItem property). The Form class has code in the OnVisibleChanged and OnMdiChildActivate methods to update the menu. Idle hope though, You can't see the menu item list change, nor can you override any of the plumbing code. Not without doing similar and overloading these methods in your own child forms.

Punt this problem, simply write a public method in your MDI parent form that you consistently use to add new child windows:

public void AddChild(Form child) {
  child.MdiParent = this;
  child.FormClosed += child_FormClosed;
  // Run your code to handle new child windows here...
}
private void child_FormClosed(object sender, FormClosedEventArgs e) {
  // Your code to handle closed child windows here...
}

You can even make AddChild static, since there's only ever one MDI parent.

Hans Passant
Thanks @Hans, I had a feeling i would have to write something like the above code.
Ben