How to close current window (in Code) when launching new Window

Just do this:

this.Close();
SignInWindow signIn = new SignInWindow();
signIn.ShowDialog();

bear in mind that will actually close the MainWindow. If all you're really trying to do is hide it, then do this:

this.Hide();
SignInWindow signIn = new SignInWindow();
signIn.ShowDialog();
this.Show();

That will hide the MainWindow while the login form is up, but then show it again when it's complete.


Okay, so apparently you're launching this form from a static class that is outside the form. That would have been pretty relevant information. But a solution would be this:

var w = Application.Current.Windows[0];
w.Hide();

SignInWindow signIn = new SignInWindow();
signIn.ShowDialog();

w.Show();

You can try this:

SignInWindow signIn= new SignInWindow();
Application.Current.Windows[0].Close();
signIn.ShowDialog();

Tags:

C#

Wpf

Xaml