C# Allow only 1 instance of a form to be opened
Tutorial Name: C# Allow only 1 instance of a form to be opened
Category: Programming Tutorials
Submitted By: Zero1UP
In this example I will show you how to only allow one instance of a form to be open when using MDI parent forms.
An MDI parent form acts as a container in which you are able to open other forms inside of. If you have a multiform application this can help with screen clutter since it will be self contained.
To make a form an MDI parent head over to the form properties and set IsMdiContainer to true.
Any form that you wish to open in this form, you would set their mdiParent to this form. However the code below will handle this for you.
//Code that handles Child forms. May be adding more code to this down the road for customization.
//No need to write the same code for every form
private void CreateMDIChild(Form childForm)
//Checks if child form already exists. Only open if it does not exist in the collection
FormCollection allForms = Application.OpenForms;
bool formOpened = false; //Assume that this form does not already exist
foreach (Form frm in allForms)
if (frm.Name == childForm.Name)
//set it to true
formOpened = true;
//As long as formOpened is false we can open the new form as a child form to the parent
if (formOpened == false)
childForm.MdiParent = this;
Using the method
private void loginRegisterToolStripMenuItem_Click(object sender, EventArgs e)
- 01. [VB.NET] How to make and sav... (520)
- 02. VB.NET] Open Site when Tool... (376)
- 03. [VB.NET] Opening external pr... (371)
- 04. [VB.NET] Message Box (432)
- 05. [ VB.NET ] How to use progre... (491)
- 06. [VB.NET] How to extend your... (567)
- 07. [VB.NET] How to make flashin... (864)
- 08. [VB.NET] How to use a progre... (782)
- 09. [VB.NET] How to Make a Splas... (811)
- 10. [VB.NET] How to Make a HTML... (491)
- 11. [VB.NET] How to Count Time! (557)
- 12. [VB.NET] How to Make a Count... (512)
- 13. [VB.NET] How to Make a Rando... (295)
- 14. [CMD] How to create lots of... (183)
- 15. CMD - How to Shutdown Comput... (600)