How to prevent multiple instances of child form in MDI windows form application in .NET

No.of Views2553
Bookmarked0 times
Downloads 
Votes0
By  kirtan007   On  05 Aug 2010 11:08:17
Tag : CSharp , Windows Forms
This article shows you how to prevent multiple instances of child form in MDI windows Form application.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

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. 

Image Loading

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.

Sample Project Source

Download source files -40 kb

 
Sign Up to vote for this article
 
About Author
 
kirtan007
Occupation-
Company-
Member Type-Senior
Location-Not Provided
Joined date-02 Jul 2009
Home Page-http://kirtan.uni.cc
Blog Page-
He completed his Bachelor of Computer Application from Gujarat University 2009 .He is doing Master of Computer Application from Gujarat Technological University right now .. His area of Interests are Web Hacking , C# .net Windows form ,asp.net , WPF ,Silverlight ,SQL Server and Some PHP.
 
 
Other popularSectionarticles
Comments
By:SurajDate Of Posted:9/29/2010 5:25:10 PM
Prevent multiple MdiChildren 2
Also, changed to compare "Name" instead of "Text" Old value: if (f.Text == "Form2") New value: if(frm.Name == "Form1") Thanks, Suraj.
By:SurajDate Of Posted:9/29/2010 5:22:19 PM
Prevent multiple MdiChildren
I replaced "Application.OpenForms" to "this.MdiChildren" and it works great. Old value: "foreach (Form f in Application.OpenForms)" New value: "foreach(Form frm in this.MdiChildren)" Thanks, Suraj.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top