Post: C# Allow only 1 instance of a form to be opened
10-19-2014, 11:25 AM #1
Default Avatar
Oneup
Guest
(adsbygoogle = window.adsbygoogle || []).push({}); This was a problem I ran into with using MDI parents /children forms. You could open the same form over and over and it soon became a mess. The code below will check to see if it already exists and if it doesn't assign the new form as a child to the mdi parent form and open it.

    
//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)
{
//Tried to open form here however it throws an error about the collection being modified. So we create a bool and if the form exists
//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;
childForm.Show();
}
}


Using the method

    
private void loginRegisterToolStripMenuItem_Click(object sender, EventArgs e)
{
CreateMDIChild(new frm_LoginRegister());
}

The following user thanked Oneup for this useful post:

theDaftDev
12-02-2014, 05:01 PM #2
Boliberrys
^^ Sexy ^^
Originally posted by 1UP View Post
This was a problem I ran into with using MDI parents /children forms. You could open the same form over and over and it soon became a mess. The code below will check to see if it already exists and if it doesn't assign the new form as a child to the mdi parent form and open it.

    
//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)
{
//Tried to open form here however it throws an error about the collection being modified. So we create a bool and if the form exists
//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;
childForm.Show();
}
}


Using the method

    
private void loginRegisterToolStripMenuItem_Click(object sender, EventArgs e)
{
CreateMDIChild(new frm_LoginRegister());
}


I was looking for this! But i get an error when i click the button :(

You must login or register to view this content.

Can you help me please?
12-02-2014, 05:25 PM #3
Default Avatar
Oneup
Guest
Originally posted by Boliberrys View Post
I was looking for this! But i get an error when i click the button :(

You must login or register to view this content.

Can you help me please?


You need to set this property on the form that you wish to have all your forms fall into.
You must login or register to view this content.
12-02-2014, 05:37 PM #4
Boliberrys
^^ Sexy ^^
Originally posted by 1UP View Post
You need to set this property on the form that you wish to have all your forms fall into.
You must login or register to view this content.


Now i get this error You must login or register to view this content.
Sorry for so many questions, im kinda noob at this
12-02-2014, 05:58 PM #5
Default Avatar
Oneup
Guest
Originally posted by Boliberrys View Post
Now i get this error You must login or register to view this content.
Sorry for so many questions, im kinda noob at this


You can't make the parent form a child of itself.

Think of it this way:

You have your main form. We will call it frm_main
This form has the property IsMdiContainer set to true since we want this to hold all of our other forms.

Now you create a second form. We will call this one frm_secondary

This is the form that you will use with the method.

So CreateMDIChild(new frm_secondary());

Don't worry about asking questions. That's what threads like these are for.

The following user thanked Oneup for this useful post:

Boliberrys
12-02-2014, 06:40 PM #6
Boliberrys
^^ Sexy ^^
Originally posted by 1UP View Post
You can't make the parent form a child of itself.

Think of it this way:

You have your main form. We will call it frm_main
This form has the property IsMdiContainer set to true since we want this to hold all of our other forms.

Now you create a second form. We will call this one frm_secondary

This is the form that you will use with the method.

So CreateMDIChild(new frm_secondary());

Don't worry about asking questions. That's what threads like these are for.

Thanks, i think this will be the last error xD
When i click on instructions (It opens the Form), but it opens the form inside my MAIN form:

You must login or register to view this content.
12-02-2014, 06:43 PM #7
Default Avatar
Oneup
Guest
Originally posted by Boliberrys View Post
Thanks, i think this will be the last error xD
When i click on instructions (It opens the Form), but it opens the form inside my MAIN form:

You must login or register to view this content.

That's what this code does. The main form is the parent and if you call the function, whatever form you put in there will open in the main form.
12-02-2014, 07:27 PM #8
Boliberrys
^^ Sexy ^^
Originally posted by 1UP View Post
That's what this code does. The main form is the parent and if you call the function, whatever form you put in there will open in the main form.


Ohh Ok! Thanks!

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo