13
Answers

How To Open Only One Child Form At A Time In MDI Form Application

Photo of Vidya Yadav

Vidya Yadav

15y
37.9k
1

I have an application with one MDI Form and I open other forms(Child Forms) through that MDI form.
But each time click the menu multiple duplicate forms get open..
I want that only one form must be open at a time..How can i do that?
Can anyone help me on that..
Thanks,
Vidya

Answers (13)

4
Photo of Praveen VR
NA 70 93.8k 12y

Please Download the sample application attached here.

Hopefully that will solve your problem.

3
Photo of Kirtan Patel
NA 21.9k 4.1m 15y
Friend,

Write Code like Below to Open the Form

it will Open The new Form if form is not open and if its Open then it will not open New Form Instead it will Focus the Opened Form

 private void button1_Click(object sender, EventArgs e)

        {

 

            FormCollection fc = Application.OpenForms;

            bool FormFound = false;

            foreach (Form frm in fc)

            {

                if (frm.Name == "Form2")

                {

                    frm.Focus();

                    FormFound = true;

                }

            }

 

            if (FormFound == false)

            {

                Form2 f = new Form2();

                f.Show();

            }

        }



All the Best

and

Check "Do you like this answer" please :)

0
Photo of Sanju Gupta
NA 2 0 9y
Thanks Bosss... I Was very worried about this.... thanks for your code
 
0
Photo of Ashok Karale
NA 19 3.1k 11y
use

forminstance.ShowDialog();
0
Photo of gaurav gumber
NA 12 0 14y
Hi dude.........

I m gaurav.......new to vb.net.........i need your help....you have given a code for mdi form for open only one instance of a form if it is already opened than the same is focused back.....and the code is in C # which is given below can you give me code for vb.net also.....and whr should i write this code in every form code or in form load..........
Please reply me on g_gumber@yahoo.com

private void button1_Click(object sender, EventArgs e)

        {

 

            FormCollection fc = Application.OpenForms;

            bool FormFound = false;

            foreach (Form frm in fc)

            {

                if (frm.Name == "Form2")

                {

                    frm.Focus();

                    FormFound = true;

                }

            }

 

            if (FormFound == false)

            {

                Form2 f = new Form2();

                f.Show();

            }

        }




And i need one more help.........when we search data from database on the base of Id which is primary key.........we can show this data in datagridview........but i want to export same result in excel format..........how can i do can send me a sample code.....


Thanks

Gaurav Gumber

g_gumber@yahoo.com




0
Photo of Johann Genutis
NA 2 0 15y

Thank you Kirtan!
Nice and simple and works!
0
Photo of minakshi
NA 55 0 15y
U can close using following code
 Form activeform_inapp = this.ActiveMdiChild;
            if (activeform_inapp == null)
            {
             
            }
            else
            {
              
                activeform_inapp.Close();
           
            }
regards,
minakshi
0
Photo of Rajeswari nathan
NA 199 24.6k 15y

The following code is used to close all the MDI child forms
foreach
(Form aFormL in this.OwnedForms)
{
aFormL.Close();
}
0
Photo of Rajeswari nathan
NA 199 24.6k 15y


See bellow code to open only one instance of the form
 
public partial class Form2 : Form
{
Form1 aFormL;
Form3 aForm3L;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (aFormL == null)
{
aFormL =
new Form1();
aFormL.Show();
}
}
private void button2_Click(object sender, EventArgs e)
{

if (aForm3L == null)
{

  aForm3L= new Form3();
  aForm3L.Show();
}

}
}
0
Photo of Vidya Yadav
NA 59 0 15y

Sorry, but i am able to implement the given code snippet.
Can u please give it in detail.
Do u know how to close all opened child windows in MDI form, it can also work in my problem.
Regards,
Vidya
 
0
Photo of Rajeswari nathan
NA 199 24.6k 15y

declare aFormL as member of the form(to access public)
Form1
aFormL;
 
in the button click
do as follows
if
(aFormL == null)
{
aFormL =
new Form1();
aFormL.Show();
}
0
Photo of Vidya Yadav
NA 59 0 15y

Thanks,
But this now don't allow me to open any other forms also.
Means more presicesly i don't want to open duplicate forms.
If Form1 is opened once then it should not be opened again but, we must be able to open Form2 aslo along with Form1.
Can u help me on this problem, it's urgent,
Regards,
Vidya
 
0
Photo of Rajeswari nathan
NA 199 24.6k 15y

When you open the form from the MDI form...
use the ShowDialog()
Following is the sample code
 
Form1 aFormL = new Form1();
aFormL.ShowDialog();
 
Form2 aFormL = new Form2();
aFormL.ShowDialog();
 
So you can open only one form at the time