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?
Asked
Active
Viewed 1,223 times
0
-
Have you tried checking if the form is already opened before showing it? – Mathieu Guindon Aug 07 '13 at 11:46
-
http://stackoverflow.com/questions/3861602/how-to-check-if-a-windows-form-is-already-open-and-close-it-if-it-is – Mathieu Guindon Aug 07 '13 at 12:06
-
`is there a way to close all other forms whenever a new form is open?` ~ doesn't that defeat the purpose of MDI? – Mathieu Guindon Aug 07 '13 at 13:25
-
i want the menu bar to be visible throughout. Can I do that without MDI? – Monty Swanson Aug 07 '13 at 14:35
2 Answers
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