Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If the user logs on successfully, then I want to show the main window, if not, I want to exit the application

Tags:

wpf

Stupidly simple question I can't figure out.

I have a WPF application, and I want to display a Logon dialog box first (before the main window shows up).

If the user logs on successfully, then I want to show the main window, if not, I want to exit the application.

How does one do this correctly?

like image 451
Sako73 Avatar asked Apr 18 '11 21:04

Sako73


2 Answers

I think I figured out what I was trying to do.

1) I needed to set the "StartupUri" in the App.xaml to "Logon.xaml", where Logon.xaml is my logon window.

2) in the LogonButton_Click event handler, I added the following

if (blnAuthenticateSuccessful) {     MainWindow main = new MainWindow();     App.Current.MainWindow = main;     this.Close();     main.Show(); } 

This seems to accomplish what I want.

like image 142
Sako73 Avatar answered Sep 21 '22 04:09

Sako73


If you wanted a new window to appear to allow the user to enter their login information, then I have added some code below. However, creating a true Modal Dialog box is a bit more complicated in WPF so I haven't explained it here. There is information about modal dialog boxes in WPF here: http://msdn.microsoft.com/en-us/library/aa969773.aspx

From the MainWindow you can open the login window and hide the main window with this:

// Code for MainWindow  // Create a new instance of the login window and then show it LoginWindow loginWindow = new LoginWindow(); loginWindow.Show();  // Hide the MainWindow until later this.Hide(); 

Then use this on the login page to show the main window again once the user has logged in:

// Code for Login window  // This code finds the main window again and shows it Application.Current.MainWindow.Show(); 
like image 22
Joel Kennedy Avatar answered Sep 19 '22 04:09

Joel Kennedy