title: Implementing Log in form in WPF C#
body: <p>I'm trying to make Log in window in my application, I'm pretty new to WPF and it's not going so well for me.</p>
  
  <p>This is what I did, and I'm not even sure is this correct way, but anyway my question is described in code comments.</p>
  
  <pre><code> /// &lt;summary&gt;
  /// Interaction logic for MainWindow.xaml
  /// &lt;/summary&gt;
  public partial class MainWindow : Window
  {
      public MainWindow()
      {
          InitializeComponent();
  
          Window_LogInScreen loginScreen = new Window_LogInScreen();
          loginScreen.ShowDialog();
  
          if (loginScreen.DialogResult == true)
          {
              MessageBox.Show("login success I should keep executing my application.."); // how could I continue my application if this condition is allright?
          }
          else
          {
              MessageBox.Show("login failed"); //how could I stop my application if I reach this code, and how to stay on LogIn_Screen form so user can enter password again..
          }
  
          lblDate.Text = DateTime.Now.Date.ToString("MM/dd/yyyy");
          lblTime.Text = DateTime.Now.ToString("HH:mm:ss");
      }
  
  
  }
  </code></pre>
  
  <p>My Window_LogInScreen :</p>
  
  <pre><code>public partial class Window_LogInScreen : Window
  {
      public Window_LogInScreen()
      {
          InitializeComponent();
          lblPasswordBox.Focus();
      }
  
  
      private void Window_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
      {
          if (e.Key == Key.Enter)
          {
              if (lblPasswordBox.Password == "1")//lets say pw is "1" for testing purposes
              {
                  this.DialogResult = true;
                  this.Close();
              }
              else
                  this.DialogResult = false;
          }
      }
  }
  </code></pre>
  
  <p>So guys how could I achieve that?</p>
  
  <p>In window forms I would do  this is Program.cs, but in WPF I'm somehow lost and I don't know how to achieve this on a right way,</p>
  
  <p>but my logic is:</p>
  
  <blockquote>
    <p>1) open LogIn screen before application run, check for password when
    user press Enter on keyboard, if password is OK then set DialogResult
    to true,</p>
    
    <p>2) go back to <code>MainWindow</code>, check there is <code>DialogResult==true</code>, if
    yes, then keep going with execution of <code>MainWindow</code>, else, stop
    application and stay on <code>LogIn_Screen</code> so user can enter password
    again..</p>
  </blockquote>
  
  <p>Thanks guys,
  Cheers</p>
  
  <p>EDIT after Xioy312 answer:</p>
  
  <p>@Xiaoy312 The code you posted is really awesome, and I'm really intrested in this code, but it looks a bit incomprehensible to me..</p>
  
  <p>Let's back to your first EDIT,</p>
  
  <pre><code>  if (e.Key == Key.Enter &amp;&amp; lblPasswordBox.Password == "1")
      {
          this.DialogResult = true;
          this.Close();
      }
  </code></pre>
  
  <p>how come <code>this.DialogResult</code> will not
  become false if condition is not satisfied? because with that code that you have posted in your answer before last edit,  <code>MessageBox.Show("login failed");</code> from <code>Application_Startup</code> would never be reached, and I'm wondering why?
  I guess if mentioned condtion (<code>if (e.Key == Key.Enter &amp;&amp; lblPasswordBox.Password == "1")</code>) is not satisfied, than DialogResult will be false and <code>MessageBox.Show("login failed");</code> 
  from else part would be called? but actualy that is not true, because here we have that failed attempt is silent, and I'm wondering why?</p>
  
  <p>And also, how come application would exit with this Behavior? Can you explain little bit what's happening behind, as much as I can se you'r assigning value to a DialogResult in case it is "1".. ? </p>
  
  <pre><code>#region Behavior 1: Exit the application on invalid password
          this.DialogResult = lblPasswordBox.Password == "1";
          this.Close();
          #endregion
  </code></pre>
  
  <p>Could you explain this code little bit, I would be so  thankful!</p>