ARTICLE

How to Prevent Multiple Instances of Child Form in MDI Windows Form Application

Posted by Kirtan Patel Articles | How do I April 11, 2010
This article shows you how to prevent multiple instances of child form in MDI windows Form application.
Reader Level:
 

Introduction


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.
   
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.

Login to add your contents and source code to this article
Article Extensions
Contents added by Jiten on Jun 02, 2010
Hi Kirtan,
Thanks for putting the code snippet here. It saved me some time on researching.
But I feel, using the name property of form instead of text is a a better solution.
E.g.
if (f.Text == "Form2") --> if (f.Name == "Form2")

Thanks again. :)

post comment
     

can you tell me how we can prevent Multiple Instances of Child Form in Mdi using wpf application

Posted by aditi ghadge Feb 26, 2013

Thanks for article it was very helpful to me.

Posted by ohrice Mar 25, 2012

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 "pavanpabolu@gmail.com" plzzz thanks pavan

Posted by sri pavan Sep 29, 2011

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

Posted by zeinab marji Jun 03, 2010

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();

}
}

Posted by zeinab marji Jun 03, 2010
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts