Is there an event I can use to tell if a child form has been added or removed from the MDI parent?
+4
A:
Yes. On your main MDI form, wire up to the MdiChildActivated Event.
Like so:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.MdiChildActivate += new EventHandler(Form1_MdiChildActivate);
}
void Form1_MdiChildActivate(object sender, EventArgs e)
{
MessageBox.Show("Activated");
}
private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
Form form2 = new Form2();
form2.MdiParent = this;
form2.Show();
}
}
And that event will fire when the child form is both activated or deactivated.
Chris Holmes
2009-02-11 18:56:58
how can I tell if it's coming or going? Just check the list of children and see if sender is there?
Malfist
2009-02-11 19:10:00
Humm, that may not work. If I reactivate/focus an open form it also throws the event.
Malfist
2009-02-11 19:11:12
I got it, an ID field in the base class for all the children.
Malfist
2009-02-11 19:14:12
Your MDI form will also have an array of children. The property is MdiChildren. You can check the count of that list to know whether a form was just added, removed, or focused (count would stay the same).
Chris Holmes
2009-02-11 19:14:35
good point! Easier to implement and maintain too
Malfist
2009-02-11 19:17:31
No, that doesn't seem to work...
Malfist
2009-02-11 19:23:24
It's because the MdiChildren collection doesn't get updated until the child form is completely gone. In other words, MdiChildActivated fires before the child goes away. So it works for detecting when a form is added, but it lags one behind when a form is removed.
Chris Holmes
2009-02-11 21:03:24
+2
A:
No, there is not. You would have to subclass Form and expose specific events that would indicate when the child is added and then route all attachments of child forms through a method that would wire up the child form, as well as raise the event.
casperOne
2009-02-11 18:59:15
@Malfist: You would extend Form, add an AddMdiChild method, and then when it is called, you would set the MdiParent of the form passed in to itself, and then fire the event.
casperOne
2009-02-11 20:35:21
@Malfist: Attach to the Closed/Closing events on the child when AddMdiChild is called.
casperOne
2009-02-11 20:39:32
But the children would have their own Closed/Closing events, can I add another event handler?
Malfist
2009-02-11 20:43:53
@Malfist: You would not be replacing it. The event handler would be added on to any that already existed.
casperOne
2009-02-11 20:50:48
+1
A:
Wire up the MdiChildActivate
event and keep a list of recognized children. When a new form is activated, also wire up the FormClosed
event.
private List<Form> ChildFormList = new List<Form>();
private void MyForm_MdiChildActivate(object sender, EventArgs e)
{
Form f = this.ActiveMdiChild;
if (f == null)
{
//the last child form was just closed
return;
}
if (!ChildFormList.Contains(f))
{
//a new child form was created
ChildFormList.Add(f);
f.FormClosed += new FormClosedEventHandler(ChildFormClosed);
}
else
{
//activated existing form
}
}
private void ChildFormClosed(object sender, FormClosedEventArgs e)
{
//a child form was closed
Form f = (Form)sender;
ChildFormList.Remove(f);
}
lc
2009-05-20 15:57:51