This is common problem for beginners while working with MDI form. Multiple instances of the form will open when we try to open it with Form.Show() method.
This article will show how to overcome with this problem.
Technology
CSharp 2.20/3.5
Implementation
For preventing multiple instances of child form we can use ShowDialog() method. But it will be problematic to user because if user wants to work with many windows at a time he can not because he need to close opened window if he opened it with ShowDialog() method.
So lets implement some code that will open new window, if there is no child window opened before. And if its opened previously then application will just focus that window and will not create any new child windows.
Understanding the code
Here we have declared one variable that is used as flag for the child window is already opened or not? This flag will store the status of child form :)
Now we iterate through each opened form in our application and check if any form with Form2 is already opened or not. If it is found, then we set flag to true and focus() that already opened form and break the loop.
Now we are checking status of flag. If application sees that flag is still false after iterating then it will create a new instance of that child window :)
That's it you are done !
Conclusion
We have just see in this article how to prevent multiple instances of child form in MDI windows Form application.
Technology
CSharp 2.20/3.5
Implementation
For preventing multiple instances of child form we can use ShowDialog() method. But it will be problematic to user because if user wants to work with many windows at a time he can not because he need to close opened window if he opened it with ShowDialog() method.
So lets implement some code that will open new window, if there is no child window opened before. And if its opened previously then application will just focus that window and will not create any new child windows.
private void button1_Click(object sender, EventArgs e)
{bool IsOpen = false;
foreach (Form f in Application.OpenForms)
{if (f.Text == "Form2")
{IsOpen = true;
f.Focus();break;
} } if (IsOpen == false)
{Form2 f2 = new Form2();
f2.MdiParent = this;
f2.Show(); }}Understanding the code
Here we have declared one variable that is used as flag for the child window is already opened or not? This flag will store the status of child form :)
Now we iterate through each opened form in our application and check if any form with Form2 is already opened or not. If it is found, then we set flag to true and focus() that already opened form and break the loop.
Now we are checking status of flag. If application sees that flag is still false after iterating then it will create a new instance of that child window :)
That's it you are done !
Conclusion
We have just see in this article how to prevent multiple instances of child form in MDI windows Form application.

Amit RaiPosted Jul 21, 2017, 1:38 AM
Hi, actually your code worked nicely, but Application.exit(); unable stop completely i.e. it was unable to stop debugging, I think your code starts thread which are unable to stop by Application.Exit();Can anyone help !
Pisi CanPosted Jan 10, 2016, 2:16 PM
Thanks.
Asif PatankarPosted Sep 16, 2015, 4:39 AM
Hi all! In my application i'm considering a primary form as parent. And the helpful code is throwing an exception at " formf2.MdiParent = this; ". Please let me know how to define a form as parent form. Thankful for responses!
Vigo VigoPosted Jun 21, 2015, 9:37 PM
How to encapsulate this method in a other class, for prevent re writte the code?
aditi ghadgePosted Feb 26, 2013, 4:45 PM
can you tell me how we can prevent Multiple Instances of Child Form in Mdi using wpf application
ohricePosted Mar 25, 2012, 1:03 PM
Thanks for article it was very helpful to me.
Sri PavanPosted Sep 29, 2011, 9:28 AM
Hi, Thanks for your article, it helps me in some issue with simple idea, which gets me trouble for long.. I have little bit of confussion on remoting concept in programming. Plz..Can u provide me sample as well as realtime application with Remoting I'm looking for your response... to this mail "[email protected]" plzzz thanks pavan
zeinab marjieditedPosted Jun 3, 2010, 4:37 PMEdited Jun 3, 2010, 4:50 PM
in my application i am opening child forms by clicking on menu strip items.when i follow ur code ,i prevent opening multiple instances,but everytime i choose a different menu item,the old form is minimized..how can i prevent that??! i am new to C# corner..i dont know if i am going to get an answer...thanx
zeinab marjiPosted Jun 3, 2010, 4:14 PM
hello,if i want to use this code in a function that i would call from each onClick instance that would send that function the form name or the form requested,so that i don't repeat the code for all button clicks and form requests,how would the code change? ...my problem is in this part : Form2 f2 = new Form2(); if i send the form name or the form itself, in what way can i create a new instance in the function ?? public void checkIfOpen(Form thisForm) { bool IsOpen = false; foreach (Form f in Application.OpenForms) { if (f.Name == thisForm.Name ) { IsOpen = true; f.Focus(); break; } } if (IsOpen == false) { what to do here ?? thisForm f2 = new thisForm(); f2.Show(); } }
JitenPosted Jun 2, 2010, 7:24 AM
Hi Kirtan, Thanks for putting the code snippet here. It saved me time on research. But I would suggest that you go for the Name property of form instead of Text property, that is a bit better solution. if (f.Text == "Form2") -> if (f.Name == "Form2") Thanks again :)