ANINDYA
asked on
How to close all Open forms at a time?
Experts
I am doing a winform application and the application is medium sized but it is opening huge number of forms and end users will have a cumbersome task to close one by one all the forms.
I have a question--- is there any way by which I can close all the open forms at a time.
Thanking you,
I am doing a winform application and the application is medium sized but it is opening huge number of forms and end users will have a cumbersome task to close one by one all the forms.
I have a question--- is there any way by which I can close all the open forms at a time.
Thanking you,
Closing the application will automatically close all the forms - or do you mean only some forms at one go ?
ASKER
Expert AndyAinscow
I mean baring the login form all other open forms should close.
Thanking you for asking
I mean baring the login form all other open forms should close.
Thanking you for asking
Hi!
Perhaps what you can do is to create a collection of forms:
using System.Windows.Forms;
public Collection<Form> OpenedForms = new Collection<Form>;
You can add this to your main application file such as Program.cs.
Whenever you opened a new form (Except your login screen), add the instance to this collection:
FrmNew frmNew = new FrmNew();
OpenedForms.Add(frmNew);
frmNew.Show();
At the Close All button or on logout, just go to close each form:
Form form = null;
while (OpenedForms.Count > 0)
{
form = OpenedForms.Items[0];
form.Close();
OpenedForms.Items.RemoveAt (0);
}
Perhaps what you can do is to create a collection of forms:
using System.Windows.Forms;
public Collection<Form> OpenedForms = new Collection<Form>;
You can add this to your main application file such as Program.cs.
Whenever you opened a new form (Except your login screen), add the instance to this collection:
FrmNew frmNew = new FrmNew();
OpenedForms.Add(frmNew);
frmNew.Show();
At the Close All button or on logout, just go to close each form:
Form form = null;
while (OpenedForms.Count > 0)
{
form = OpenedForms.Items[0];
form.Close();
OpenedForms.Items.RemoveAt
}
Is this any use ( run from the main form? depends on your application structure)
foreach(Form frm in this.OwnedForms)
frm.Close();
foreach(Form frm in this.OwnedForms)
frm.Close();
ASKER
Expert VincentSG
please see in the attached image that it is showing that static keyword is supposed to be changed.
Thanking you
please see in the attached image that it is showing that static keyword is supposed to be changed.
Thanking you
ASKER
Have you tried my code? If it works then there is no need to do anything yourself (it is already done for you in the background)
ASKER
Expert AndyAinscow
I am sorry to ask you a few questions.
1. where your code
foreach(Form frm in this.OwnedForms)
frm.Close();
is to be put? you have written from the main form but it is showing errors there .
I am having Form_Login as the Startup form .
Now where actually I am supposed to put that code sir?
Thanking you
Take regards
Anindya
I am sorry to ask you a few questions.
1. where your code
foreach(Form frm in this.OwnedForms)
frm.Close();
is to be put? you have written from the main form but it is showing errors there .
I am having Form_Login as the Startup form .
Now where actually I am supposed to put that code sir?
Thanking you
Take regards
Anindya
Yes, in the login form - that is the main FORM of your app. It should close all forms owned by the login form (and each of those forms should then close any child forms automatically).
Hi!
Can you do this instead?
public static Collection<Form> OpenedForms = new Collection<Form>;
Then in all instances where you need to open the form, you can use
Program.OpenedForms.Add(fr m);
Can you do this instead?
public static Collection<Form> OpenedForms = new Collection<Form>;
Then in all instances where you need to open the form, you can use
Program.OpenedForms.Add(fr
In your Form_Login, add a button or menu-item to close all other open forms.
Then put the following code in the event handler for that button:
Dim formsToClose As New List(Of Form)
For Each f As Form In Application.OpenForms
If f IsNot Me Then
formsToClose.Add(f)
End If
Next
For Each f As Form In formsToClose
f.Close()
Next
Then put the following code in the event handler for that button:
Dim formsToClose As New List(Of Form)
For Each f As Form In Application.OpenForms
If f IsNot Me Then
formsToClose.Add(f)
End If
Next
For Each f As Form In formsToClose
f.Close()
Next
ASKER
Expert AndyAinscow and Expert joriszwaenepoel
I would like to tell you that how my application is working and why your code is not applicable.
Sirs after the login page is coming the Form_DashBoard form and I am opening by
db.ShowDialog();
Not only that from dashboard whatever form I am opening are also by virtue of the ShowDialog()
It is as per my requirement.
Now obviously I can not touch the login form once the other Forms are open .
I hope now the matter is clear to you experts.
Thanking you sirs for asking
Take regards
Anindya
I would like to tell you that how my application is working and why your code is not applicable.
Sirs after the login page is coming the Form_DashBoard form and I am opening by
db.ShowDialog();
Not only that from dashboard whatever form I am opening are also by virtue of the ShowDialog()
It is as per my requirement.
Now obviously I can not touch the login form once the other Forms are open .
I hope now the matter is clear to you experts.
Thanking you sirs for asking
Take regards
Anindya
Not sure about how you want your end user experience to appear, but you could make use of Multiple Document Interface (MDI). By using this approach, all forms opened from your main form, which I guess is the "FormLogin1", would be accessible from the ForEach approach suggested by AndyAinscow above.
There are numerous tutorials out there on MDI applications, and here are two short samples:
http://www.c-sharpcorner.com/UploadFile/ggaganesh/DevelopingMDIAppplicationsinCSharp11272005225843PM/DevelopingMDIAppplicationsinCSharp.aspx
http://www.codeproject.com/KB/cs/mdiformstutorial.aspx
If you would like to try this approach you should set the parameter IsMdiContainer=true in your main form ("FormLogin1"), and create/open all secondary forms in this way:
Form frmchild=new Form();
frmchild.MDIParent=this;
frmchild.Show();
When you are ready to close all forms you just close the main form, or call this method to close all child forms withour closing the main form:
foreach (Form chform in this.MdiChildren) {
form.Close();
}
There are numerous tutorials out there on MDI applications, and here are two short samples:
http://www.c-sharpcorner.com/UploadFile/ggaganesh/DevelopingMDIAppplicationsinCSharp11272005225843PM/DevelopingMDIAppplicationsinCSharp.aspx
http://www.codeproject.com/KB/cs/mdiformstutorial.aspx
If you would like to try this approach you should set the parameter IsMdiContainer=true in your main form ("FormLogin1"), and create/open all secondary forms in this way:
Form frmchild=new Form();
frmchild.MDIParent=this;
frmchild.Show();
When you are ready to close all forms you just close the main form, or call this method to close all child forms withour closing the main form:
foreach (Form chform in this.MdiChildren) {
form.Close();
}
Do you still face any errors with the new code?
Since you are using ShowDialog, your only option is to close the forms one by one.
Is this some kind of "wizard"? Maybe you can change your design to close the previous form before showing the next one?
dim owner = me.Owner
Me.Close()
dim fNext as new FormNext
fNext.ShowDialog(owner)
Or when the user closes the last form, you could close the previous form automatically, something like this:
dim fNext as new FormNext
fNext.ShowDialog(owner)
Me.Close
Is this some kind of "wizard"? Maybe you can change your design to close the previous form before showing the next one?
dim owner = me.Owner
Me.Close()
dim fNext as new FormNext
fNext.ShowDialog(owner)
Or when the user closes the last form, you could close the previous form automatically, something like this:
dim fNext as new FormNext
fNext.ShowDialog(owner)
Me.Close
So, run the code from Form_DashBoard
ASKER
Expert VincentSG
I am still trying your code
I have question can you please tell me where you suggested me to put this code
Form form = null;
while (OpenedForms.Count > 0)
{
form = OpenedForms.Items[0];
form.Close();
OpenedForms.Items.RemoveAt (0);
}
I have put it in a form which is at the third layer( that is first one is login form then Dashboard form then
Form_CompanyBusinessOpport unity.cs Now there I have put a label "Logout" and onclick of that I have put
Form form = null;
while (OpenedForms.Count > 0)
{
form = OpenedForms.Items[0];
form.Close();
OpenedForms.Items.RemoveAt (0);
}
is it wrong .
or you mean something else.
If I am correct then I would like to tell you that it is sowing an error that you can see in the attached image.
If I am wrong then please suggest me where I am wrong.
Thanking you,
take regards
Anindya
1.JPG
I am still trying your code
I have question can you please tell me where you suggested me to put this code
Form form = null;
while (OpenedForms.Count > 0)
{
form = OpenedForms.Items[0];
form.Close();
OpenedForms.Items.RemoveAt
}
I have put it in a form which is at the third layer( that is first one is login form then Dashboard form then
Form_CompanyBusinessOpport
Form form = null;
while (OpenedForms.Count > 0)
{
form = OpenedForms.Items[0];
form.Close();
OpenedForms.Items.RemoveAt
}
is it wrong .
or you mean something else.
If I am correct then I would like to tell you that it is sowing an error that you can see in the attached image.
If I am wrong then please suggest me where I am wrong.
Thanking you,
take regards
Anindya
1.JPG
Hi ANINDYA,
Actually your code is ok, just remember to replace
OpenForms
with
Program.OpenForms
since you are "hosting" the method at Program class.
You can put this code anywhere like what you are doing now.
OR you can have a method at the Program.cs.
public static void CloseAllForms()
{
Form form = null;
while (OpenedForms.Count > 0)
{
form = OpenedForms.Items[0];
form.Close();
OpenedForms.Items.RemoveAt (0);
}
}
Then from your logout method or anywhere else, call
Program.CloseAllForms();
Actually your code is ok, just remember to replace
OpenForms
with
Program.OpenForms
since you are "hosting" the method at Program class.
You can put this code anywhere like what you are doing now.
OR you can have a method at the Program.cs.
public static void CloseAllForms()
{
Form form = null;
while (OpenedForms.Count > 0)
{
form = OpenedForms.Items[0];
form.Close();
OpenedForms.Items.RemoveAt
}
}
Then from your logout method or anywhere else, call
Program.CloseAllForms();
ASKER
Expert VincentSG
would you please tell me why OpenedForms is inaccessible in the code.
Please see the attached image.
Thanking you for reply,
Anindya
1.JPG
would you please tell me why OpenedForms is inaccessible in the code.
Please see the attached image.
Thanking you for reply,
Anindya
1.JPG
Is there some reason you don't want to use the BUILT IN functionality to get the open forms in your application?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Expert VincentSG
Your code is working perfectly and is giving the exact required O/P.
Sir actually I am very sorry to give you a lot of pain but the reason is that I am a fresher so my concepts are not too clear .
Thanking you very very very much .
I can not explain to you how much you have helped me .
Hope to get your help in future too.
Take regards,
Thanking you,
Anindya Chatterjee
Bangalore
India
Your code is working perfectly and is giving the exact required O/P.
Sir actually I am very sorry to give you a lot of pain but the reason is that I am a fresher so my concepts are not too clear .
Thanking you very very very much .
I can not explain to you how much you have helped me .
Hope to get your help in future too.
Take regards,
Thanking you,
Anindya Chatterjee
Bangalore
India
Glad to have solved your problem. Cheers!