Close application after closing dialog window - c#

Sorry for (maybe) easy question, but how can I shut down application, when I close dialog window ( which I called before running MainWindow) ?
As I mentioned above, before running MainWindow in my application I call a Dialog window. In App.xml I added this:
Startup="Application_Startup"
Then in App.xml.cs I wrote this:
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
StartWindow startWindow = new StartWindow();
startWindow.ShowDialog();
}
}
In my StartWindow there are only two exits - connect button, that worked fine, and classic closing button on title bar. I would like this button to shut down my application.
In my class StartWindow I wrote this:
private void Window_Closed(object sender, EventArgs e)
{
DialogResult = false;
}
But it still runs my MainWindow after closing my StartWindow. I even tried
private void Window_Closed(object sender, EventArgs e)
{
DialogResult = false;
Application.Current.Shutdown();
}
But still. Even if in above code I changed rows.
What I am doing wrong? I suspect, that is very easy to program what I want, but I don't know how. Yet. Can you help me, please?

ShowDialog() has a return value.
bool? dialogResult = startWindow.ShowDialog();
if (dialogResult == false)
{
...
}
You have to set DialogResult in your StartWindow before closing it, but you have already done that.

Related

How Do I Call Another Window With Button Press?

I want to be able to press a button and have the program open up a new window and close the old one.
I have followed solutions from this link but i have never has success with any of them How do I open a second window from the first window in WPF?
Here is my work soo far:
Window editor = new Window();
editor.Show();
this.Close();
But this does nothing.
The program should open up a new window and close the old one.
The functionality you described will work just fine. The Problem there is would more likely be the function or Methode in which you call this function.
To write a Methode that would handle a Button press as you want is pretty good described here: https://www.c-sharpcorner.com/forums/c-sharp-button-click-hold-and-release.
Hopefully, this will help you otherwise just ask
here is a small Implementation if that helps:
public partial class MainWindow : Window
{
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
Window editor = new MainWindow();
editor.Show();
this.Close();
}
private void MainWindow_KeyUP(object sender, KeyEventArgs e)
{
}
public MainWindow()
{
this.KeyDown += MainWindow_KeyDown;
this.KeyUp += MainWindow_KeyUP;
}
}
You have to call the second window from the first. This is something I did for a project where it popped up a new login panel window:
private void displayLoginPanel_Click(object sender, EventArgs e)
{
LoginPanel myLogin = new LoginPanel(this);
myLogin.Show();
this.Hide();
}
I used hide() instead of close() because you can see that I am sending a reference of the parent to the child LoginPanel in order to come back later. You can replace the Hide() with Close().

c# make a program minimize to task bar

I am trying to make my application minimize to task bar/tray
Here is my code so far that I have pulled together from other SO posts and other people seem to have it working but my app minimizes to the tray but when I click it in the tray it doesn't reopen.
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
just to explain again the problem is the application minimizes to the tray but when I click on the icon it doesn't restore the app to normal. Instead it does nothing.
I found my problem.
what I didn't do was this
Set the NotifyIcon's visible property to false in the property editor. Now go to the property editor of Form1, click on the little lightning symbol to access the events, and double click on the Resize event, and change the code to:
and I also didn't do this
Finally we need the code to make the program show up again when the icon is double clicked. So double click on NotifyIcon1 in the designer,
I found this information here
Dreamincode
This is what I have used in the past but I fund a copy online at the link below
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
found here: http://www.codeproject.com/Articles/27599/Minimize-window-to-system-tray

How to make Close button work as Minimize: C# Winforms

How can I make the (default) Close button (at top right corner) in my application, to work it as Minimize.
Actually I want to minimize the application on clicking the cross-symbol, but exit the application, when use clicks on my menu option Exit.
I wrote this code for minimizing the form on clicking close button:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (minimize_on_close == "Yes")
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
}
}
and wrote this code for exiting the application, on clicking exit from menu options.
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
Application.Exit();
}
But now, when I click on Exit menu option, then also the form is being minimized, and not exiting.
Can anyone please help?
Check to see whether FormClosingEventArgs.CloseReason is equal to CloseReason.UserClosing before deciding to minimize the window. Alternatively, compare for CloseReason.ApplicationExitCall.
From the documentation for CloseReason:
Members
...
UserClosing
The user is closing the form through the user interface (UI), for example by clicking the Close button on the form window, selecting Close from the window's control menu, or pressing ALT+F4.
...
ApplicationExitCall
The Exit method of the Application class was invoked.
Try this
EDIT
May use Resize Event to do it,
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
this.Hide();
}
Then using the FormClosing Event to cancel Close and minimize the Form as below
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
}
}

logout the main form and show the login form

i know this problem may sound stupid, but sad case, i've been searched online to get solution but still cannot get it correct. My problem now is = Logout button so to exit the main form, then show up the login form again.
The code below will NOT show the login form after i click the logout button, it straight away exit the whole application.
void logoutbtn_Click(object sender, EventArgs e)
{
CloseSockets();
this.Close();
serverlogin login = new serverlogin();
login.Show();
}
So, i try to replace this.Hide() instead of this.Close();. But, something even dumb happen. Yes, the login page show after i click the logout button, but when i click the Cancel button at my login form, it didnt exit the whole application where it suppose to exit the whole application. i guess is because the main form is just hiding and not yet close??? Plus, when i try to login again, the login button is also, NOT functioning, cannot login to main page.
i apologize upon my explanation and please tell me if it is very unclear.
Please kindly help me. Thank you so much.
You need to define 2 event in your form which will be fired on butttons click and handle it in the main form:
MainForm.cs
void logoutbtn_Click(object sender, EventArgs e)
{
CloseSockets();
this.Hide();
serverlogin login = new serverlogin();
login.Login += new EventHandler(serverlogin_Login);
login.Cancel += new EventHandler(serverlogin_Cancel);
login.Show();
}
private void serverlogin_Login(object sender, EventArgs args)
{
this.Show();
// do login
}
private void serverlogin_Cancel(object sender, EventArgs args)
{
Application.Exit();
// do exit
}
LoginForm.cs
public event EventHandler Login;
public event EventHandler Cancel;
private void OnLogin()
{
if (Login != null)
Login(this, EventArgs.Empty);
}
private void OnCancel()
{
if (Login != null)
Login(this, EventArgs.Empty);
}
private void btnLogin_Click(object sender, EventArgs e)
{
this.OnLogin();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.OnCancel();
}
You might just want to start a new instance of your application, then exit the old one, when "logout" is selected. This cleans up any resources still in use, and makes it much more difficult to leak data from one user session to the next.
The disadvantage, of course, is that it will be slower, but there's ngen.exe to reduce the cost of restarting the app.
I reviewed the first answer, and I found out that there is something wrong. In the code, he closes the form after creating the new thread. I tested it, but it always closed my form. So I switched the this.Close(); with t.Start(); and it worked. Below you have the explanation of the code.
You create a new thread then you close the form you are in (for exemple the menu), and finally you start your new thread. You create a new method where you run your new form. I commented it line by line.
private void btnLogout_Click(object sender, EventArgs e)
{
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(OpenLoginForm)); //you create a new thread
this.Close(); //you close your current form (for example a menu)
t.Start(); //you start the thread
}
public static void OpenLoginForm()
{
Application.Run(new LoginForm()); //run your new form
}
private void btnLogout_Click(object sender, EventArgs e)
{
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(OpenLoginForm));
t.Start();
this.Close();
}
public static void OpenLoginForm()
{
Application.Run(new LoginForm());
}

WPF hide a window in Closing event prevents from application terminating

A simple question again.
I am using a window in a WPF as a child window, where I would rather have the 'X' button hide the window instead of close. For that, I have:
private void Window_Closing(object sender, CancelEventArgs e) {
this.Hide();
e.Cancel = true;
}
The problem is that when the parent window is closed, this never closes and keeps the app alive.
Is there a clean way to deal with this? I thought of adding a Kill flag to all my user controls (windows):
public bool KillMe;
private void Window_Loaded(object sender, RoutedEventArgs e){
KillMe = false;
}
private void Window_Closing(object sender, CancelEventArgs e) {
this.Hide();
if (!KillMe) e.Cancel = true;
}
Then in MainWindow_Closing() I would have to set all window KillMe flags to true.
Any better way than creating additional flags and forgetting to set them before closing?
You could call Shutdown in the "parent's" closing handler... This will cause your Cancel to be ignored.
From Window.Closing:
If Shutdown is called, the Closing event for each window is raised. However, if Closing is canceled, cancellation is ignored.
I usualy have my own AppGeneral static class for such cases. When I'm realy exiting app I'm setting AppGeneral.IsClosing static bool to true. And then, when closing:
private void Window_Closing(object sender, CancelEventArgs e) {
if (!AppGeneral.IsClosing)
{
this.Hide();
e.Cancel = true;
}
}
Also, you can kill the your own process (that's ugly but working :) ) Process.GetCurrentProcess().Kill();
You should use
Application.Current.Shutdown();
inside your master window closing method.
This should override canceling of any subwindow!

Categories

Resources