Bakri Alkhateeb
Bakri Alkhateeb

Reputation: 500

How to move from one window to another in WPF?

I'm working on a WPF UI and for now I have a login window and a MainWindow with multiple userControls inside the MainWindow, now when the app starts it opens the login window, how can I move to the MainWindow and close the login window when I click on the login button??

Upvotes: 2

Views: 721

Answers (1)

Rand Random
Rand Random

Reputation: 7440

Simply close the login window and open the main window.

eg. on a Button Click

private void LoginButton_Click(object sender, EventArgs args)
{
     this.Close();
     (new MainWindow()).Open();
}

The only thing to keep in mind is the specified ShutdownMode the default is OnLastWindowClose

https://learn.microsoft.com/en-us/dotnet/api/system.windows.application.shutdownmode?view=netframework-4.8

If you want to stick with the default ShutdownMode you could do the following

private void LoginButton_Click(object sender, EventArgs args)
{
     this.Hide(); //first hide the LoginWindow
     (new MainWindow()).Open();
     this.Close(); //now close the LoginWindow, and the "MainWindow" will be the LastWindow
}

Upvotes: 1

Related Questions