0

I am trying to create a menu driven application using MDI forms. My problem is that the menu in the MDI parent will create a new child every time i clicked on it. How do i allow only one particular instance of the child to be open for a particular form but allow multiple forms from different menu to be open. For example, I would want a child from "File" to be open along with an "Edit" child. Also, is there a way to close all other forms whenever a new form is open?

Monty Swanson
  • 695
  • 16
  • 41

2 Answers2

0

You can do all of that my checking the MdiChildren array of your main form. That array will list all of the open MDI children on your form.

You can determine if an instance of a form is open by looping through the array and checking if a form of the type requested is already open.

To close all open forms, just loop through MdiChildren and call Close on all of the forms.

shf301
  • 31,086
  • 2
  • 52
  • 86
0

You can check for an existing instance of your form through the MDIChildren collection:

private void form2ToolStripMenuItem_Click(object sender, EventArgs e) {
  Form2 f = this.MdiChildren.OfType<Form2>().SingleOrDefault();
  if (f == null) {
    f = new Form2();
    f.MdiParent = this;
    f.Show();
  } else {
    f.BringToFront();
  }
}

If you want to close any previous open form, you can also just go through the MDIChildren collection:

if (f == null) {
  while (this.MdiChildren.Count() > 0) {
    this.MdiChildren[0].Dispose();
  }
  // etc...
LarsTech
  • 80,625
  • 14
  • 153
  • 225