c# Application exits when Main form closes - c#

I'm writing a multi form application, and I'd like to be able to close Form1 with out the application closing.
Right now i'm using this.Hide to hide the form. Is there a way to set it so i can close any form, or the application should only exist when all the forms are closed?
I think i remember seeing something like that at one point, but that might not have been visual studio and c#.

In your Program.cs file you have a line like Application.Run(new Form1()). Replace it with
var main_form = new Form1();
main_form.Show();
Application.Run();
Also you need to close application explicitly by call Application.Exit() where you want.

One strategy is to do something like the following:
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (Application.OpenForms.Count == 0)
Application.Exit();
}
}
If you place this on all of your forms, when the last one closes, the application will exit.

A multiform aplication should have a clear EXIT option (either by menu, toolbar), since you can not know that the user wants to close the program when the last window is closed (i supose that the user could go to the File/Open and open new windows)
An aplication that does something automatically that the user did not asked for can used frustration/confusion and spend time reopening the aplication.
Even the user can think that the application somehow crashed since he did not close it.

Based on your coments it's a single flow kind of application
I suggest that you implement the typical wizard interface on a single form with the BACK, NEXT, CANCEL buttons.
When the desired state has been reached, for example enough information has been gathered , the NEXT button is changed to a FINISH button.
Any time the user presses the CANCEL/FINISH button the window will close
And if you still want multiform you could still have it, in this you could have multiple flows at the same time and just finish or cancel the one that you want.

Related

How to close the app when I make click on X button in a child form?

I have a form app with two Forms. In the second form I have in the right corner the x button. How I can make when I make click on this button to close the app, not just hide the Form2 window?
First you need to catch the event. To do that, set an event handler on the child form's FormClosing event.
Then there are several options:
"Brute force" termination using Process.Kill().
This will terminate the process without letting any cleanup code to run. It has an effect like ending a process through the task manager. You can get the current process with Process.GetCurrentProcess. Use like this:
Process.GetCurrentProcess().Kill();
"Gentle" termination by way of closing all windows using Application.Exit().
This will close all message pumps and windows, but will do so while allowing normal cleanup code to run. It does not however guarantee the process will be terminated, for example if a forgound thread is still active after message loops are done. Use like this:
Application.Exit();
Communicate intentions to the main thread.
This is a design solution, not a "line of code" you put somewhere. The idea is that the 2 classes (of the 2 forms) have some communication mechanism (via message, events or whatever you see fit and probably already use), and the child form notifies the parent form the user wants the exit the application. Then it's up to the main form to cleanup everything, close all forms (itself and others), and exit the process normally. This is the cleanest and preferred method, but requires a proper design and more code.
If you are using form the simplest is using
this.Close(); or Application.Exit();

WPF accessing opened print dialog and close them

I have a WPF application, which needs to log out user after 5 min of inactivity.
But if user open a print dialog of any page, and do not touch screen for 5 minutes,
even if I log out user and clear all child elements, print dialog still stays on top of WPF form and somebody can come and continue to print what ever page user stayed.
I tried to use;
Window window = Application.Current.MainWindow;
or
FocusManager.GetFocusedElement();
but could not achieve to access to PrintDialog and close it.
Is there any way to access it and close if user did not respond to print dialog?
I fixed this weird problem by using
white project.
http://white.codeplex.com/wikipage?title=Working%20with%20window&referringTitle=Programming%20using%20white
By using application class, I am able to access all ModalDialogs in WPF project, and close them.
Application application = White.Core.Application.Attach(Process.GetCurrentProcess().Id);
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
White.Core.UIItems.WindowItems.Window window = application.GetWindow("MainWindow");
List<White.Core.UIItems.WindowItems.Window> modalWindows = window.ModalWindows();
foreach (White.Core.UIItems.WindowItems.Window modalWindow in modalWindows)
{
modalWindow.Close();
}
}
You can use p/invoke for this.
Use findwindow to find any window and destroywindow to close it.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx
If I understand correctly, you're able to do the user logout at the 5 minute mark (I presume via the timer event handler), but if the print dialog was open, then the printout can still end up happening.
The question is, what's really the important thing here, closing the dialog, or preventing the subsequent printout? Also, are you logging out the user but keeping the program going, or is the program quitting?
If the app is supposed to shut down, then your code can call the Application object's Shutdown() method, which will close any modal dialogs as part of the shutdown process.
If the app is supposed to keep running without a current user, then I would think the White library is your best bet. It's unclear why you've moved away from this.
With or without closing the print dialog, however, the printing code should check to see if the user is still logged in before it actually prints something. If the login expired while the print dialog was on screen, the printing code should simply exit without printing anything.

How I can close a 1st form without closing the full application? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I close a login form and show the main form without my application closing?
How i can close a 1st form with out closing full the application?
C#. as a example if my 1st form is logging form after enter username and password if they are correct that form should be close and other form need to be open.
I tried this.close() but my full application is exiting :(. After that I tried this.Hide() it works but my form is still running just hidden from UI.
ty
It is actually quite simple. On your Program.cs Main static method use the following code to launch your LoginForm.
var myLoginForm = new LoginForm();
myLoginForm.Show();
Application.Run();
Then from your LoginForm, simply remember to launch your next Form before closing it like this:
var myNextForm = new NextForm();
myNextForm .Show();
this.Close();
If you rather just want to close the application after login fails just do the following:
this.close();
Application.Exit();
Hope it helps!
In your Program.cs, you should have it create an instance of the form you wish to be long running instead of just the login form.
You can then have the other form display the login form to get that information from the user and when it exists you'll be back to your other form.
That is because the window you are closing is the main application's window. Closing that window will shut down the whole application.
Now here is the fix:
Create a second Form, LoginForm for example
On the main form load event handler (add one if there isn't any), show the LoginForm to ask the user to enter his/her credentials.
Once you check those credentials you can either return from the event handler if the credentials are good and in this case the main window will show up and the application will run normally or close the main window and the whole application will shut down.
When working with forms, what you have to realize is that calling Application.Run() just switches the applications main thread to the forms UI thread. When the form calls Close(), it will finish up whatever processing it needs to do and continues from the Application.Run() call. What you can do after this is run another Application.Run() on the next form if some property of your login form is set (frmLogin.LoginSuccessful or some such).
However, this is not best practice (IMO). What you should do is instead have the login form opened from within your main form, and then the main form will continue to run after the login form closes.

Can we and how to open a new window and closes the previous window in the same thread?

I am asking this, because i want to know if when we are running an app, for start if we have an window to authenticate like a Log In window, after validating the user, can we open the Main Window in the same Thread without creating a new one?
I am trying to do this in WPF, but i think that is same thing in WPF or in Windows Forms.
Yes, you can.
Just do it.
When you generate a Windows Forms application via the IDE, it will generate the code for one form, as well as a Main function that displays the form at runtime. You can rewrite the Main method so it displays one form modally then displays the next form.
But there's a simpler way to achieve your objectives:
Have two windows: your Main window, where most of the work is done, and the login screen.
In the OnLoad event of your main window, create an instance of your login window and call ShowModal() on this instance.
If the login fails, then exit the application.
This question does not offer enough context to tell you how to do this in your specific case. In general you can just Close() a window, construct a new one and call Show() on it.
You should make sure the Application.ShutdownMode does not kill off your application when the window is closed though.

Handle PocketPC's Task Manager Stop Running programs when using nested Forms

I have a PocketPC C# application written in Visual Studio 2005. It uses nested forms (the user is presented with a form with multiple buttons, when the user selects one a new form is opened).
I've added code so that the 1st form sets it's title to string.Empty to hide it from the Running Programs List. When the 2nd form is showing and the user uses the task manager to stop my app, the 2nd form gets the on close event.
Is there any way of knowing that the close event has come from the task manager so that I can close my application? At the moment when breakpointing the close event, I'm seeing the DialogResult being set as DialogResult.OK (Which isn't helpful) and the 2nd dialog is closed returning control to caller which thinks the user selected OK and opens the next dialog.
I've Googled for info but all the helpful code such as ClosingEventArgs aren't available in the compact framework. Any ideas?
I may be missing something but if your problem is distinguishing between the 2nd dialog being closed normally, and being closed using task manager, can you not set some kind of marker when the normal close action occurs, before closing? Logically then any close event where the marker has not been set will be down to the task manager?

Categories

Resources