close all opened windows
-
Hello all, Could somebody tell me how can i close all the windows opened in my windows application. The flow is like this from login to main window from main window to all other windows. when i click the logout button in main window it should close all the opened windows in my application and the login window must be shown.. Thanks in Advance..
-
Hello all, Could somebody tell me how can i close all the windows opened in my windows application. The flow is like this from login to main window from main window to all other windows. when i click the logout button in main window it should close all the opened windows in my application and the login window must be shown.. Thanks in Advance..
The easiest way is to make you login form your default form. When the user logs in correctly, show your "real" main form, and Hide the Login form. Then all you have to do is chain into the various close events: Login Form (on login ok):
MainForm mf = new MainForm(); mf.Show(); mf.FormClosing += new FormClosingEventHandler(mf\_FormClosing); Hide();
Login Form (event handler):
void mf\_FormClosing(object sender, FormClosingEventArgs e) { Show(); }
At this point, you main form will show when you log in, and the login form will reappear when the main form closes - so your logout button just needs to call "Close()". Then when you construct each child form, give it a single parameter constructor:
public ChildForm(Form parent) { InitializeComponent(); if (parent != null) { parent.FormClosing += new FormClosingEventHandler(ParentForm\_FormClosing); } } void ParentForm\_FormClosing(object sender, FormClosingEventArgs e) { Close(); }
Then, when the forms parent close, so will the child. So, construct your child forms as necessary:
ChildForm cf = new ChildForm(this); cf.Show();
And when the main form closes, the child forms will also close, and the log in screen be re-displayed.
If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.
-
Hello all, Could somebody tell me how can i close all the windows opened in my windows application. The flow is like this from login to main window from main window to all other windows. when i click the logout button in main window it should close all the opened windows in my application and the login window must be shown.. Thanks in Advance..
Are you aware of
Application.OpenForms
? :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
Are you aware of
Application.OpenForms
? :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
Sorry i am not aware of application.openforms. Can i get the name of the open windows using this property.
You get an array of Form references, as the documentation would tell you. :|
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
Hello all, Could somebody tell me how can i close all the windows opened in my windows application. The flow is like this from login to main window from main window to all other windows. when i click the logout button in main window it should close all the opened windows in my application and the login window must be shown.. Thanks in Advance..
Mohan, try this. Call this method in the Button Click event. public static void CloseAllForms() { //Create a Collection to Store all Opened Forms. List<Form> formsList = new List<Form>(); //All all opened forms into a Collection. foreach (Form frm in Application.OpenForms) { //Execulde the Current Form. if (frm.Name == "Form1") continue; else formsList.Add(frm); } //Now Close the forms foreach (Form frm in formsList) { frm.Close(); } }