Windows Develop Bookmark and Share   
 index > Windows Forms General > Working with multiple forms
 

Working with multiple forms

Just a quick question which I am hoping somebody can help me with.

Currently I have 4 forms called frm1 -> frm4

Each form opens the next form using the following code until all four forms are open on the screen with the latest one, frm4, being the modal form.

using (Form(2-4)Type frm(2-4)  = new Form(2-4)Type())

{

frm(2-4).showDialog();

}

What I would like is for frm2toopen from frm1 and frm3 to open from frm2 buth when frm 4 is generated from frm3 I would like forms 2 & 3 to close leaving just 1 & 4. Can anybody help? If I try to close or hide any of the forms then any previous forms in the chain disappear completely.

Thank you to anybody taking the time to read this.

-Gary
G Whyman  Monday, September 14, 2009 12:10 PM
Just keep track of your form instances explicitly:

Form2 mForm2;

private void button1_Click(object sender, EventArgs e) {
if (mForm2 == null) {
mForm2 = new Form2();
mForm2.FormClosed += new FormClosedEventHandler(mForm2_FormClosed);
}
mForm2.Show();
}

void mForm2_FormClosed(object sender, FormClosedEventArgs e) {
mForm2 = null;
}

Now you can automatically close a form as needed:

private void button2_Click(object sender, EventArgs e) {
if (mForm2 != null) mForm2.Close();
new Form3().Show();
}


Hans Passant.
nobugz  Monday, September 14, 2009 12:44 PM
Use a static class to keep track of the form instances. This functionality is already provided by the Application.OpenForms property. It's buggy but it might work for you.

Hans Passant.
nobugz  Monday, September 14, 2009 1:45 PM
Just keep track of your form instances explicitly:

Form2 mForm2;

private void button1_Click(object sender, EventArgs e) {
if (mForm2 == null) {
mForm2 = new Form2();
mForm2.FormClosed += new FormClosedEventHandler(mForm2_FormClosed);
}
mForm2.Show();
}

void mForm2_FormClosed(object sender, FormClosedEventArgs e) {
mForm2 = null;
}

Now you can automatically close a form as needed:

private void button2_Click(object sender, EventArgs e) {
if (mForm2 != null) mForm2.Close();
new Form3().Show();
}


Hans Passant.
nobugz  Monday, September 14, 2009 12:44 PM
Thanks for the response,

I am new to c# so am a little confused, if form 2 spawns 3 and 3 spawns 4, I can use your solution to close both 2 & 3 when 4 is created?

To me it looks like all the code is contained within one form at present not across 3.

Again sorry for the questions but I am new.

Regards

Gary
G Whyman  Monday, September 14, 2009 1:40 PM
Use a static class to keep track of the form instances. This functionality is already provided by the Application.OpenForms property. It's buggy but it might work for you.

Hans Passant.
nobugz  Monday, September 14, 2009 1:45 PM

You can use google to search for other answers

Custom Search

More Threads

• Problem with using class??
• DataGrid View Column Grouping
• change the Icon of a UserControl.?
• find window size
• SQL Server option missing from Data Source window. Anyone know how to fix this?
• Registry Access Permissions (RegistryKey class)
• Alpha transparency for a form
• Button text alignment Bottom
• problem with class Collection
• Shadow around TabControl border