title: WPF TextBox.Focus() annoyance
body: <p>I'm very new to .Net and WPF and have a problem. The code is a snippet. I have <code>TextBox</code>es to enter dates. I check on correct input using <code>GotFocus</code> and <code>LostFocus</code> events.</p>
  
  <pre><code>&lt;TextBox Name="sdDay" Width="40" Text="Day" GotFocus="DateDay_GotFocus" LostFocus="DateDay_LostFocus" Padding="5,5,5,5" HorizontalContentAlignment="Center" Focusable="True"/&gt;
  &lt;TextBox Name="sdMonth" Width="50" Text="Month" GotFocus="DateMonth_GotFocus" LostFocus="DateMonth_LostFocus" Padding="5,5,5,5" Margin="5,0,0,0" HorizontalContentAlignment="Center" Focusable="True"/&gt;
  &lt;TextBox Name="sdYear" Width="50" Text="Year" GotFocus="DateYear_GotFocus" LostFocus="DateYear_LostFocus" Padding="5,5,5,5" Margin="5,0,0,0" HorizontalContentAlignment="Center" Focusable="True"/&gt;
  </code></pre>
  
  <p>And the code:</p>
  
  <pre><code>private void DateDay_GotFocus(object sender, RoutedEventArgs e)
      {
          if (((TextBox)sender).Text == "Day")
              ((TextBox)sender).Text = string.Empty;
      }
  
  private void DateDay_LostFocus(object sender, RoutedEventArgs e)
      {
          if (((TextBox)sender).Text == string.Empty)
              ((TextBox)sender).Text = "Day";
          else
              CheckForCorrectDateDay((TextBox)sender);
      }
  
  private void CheckForCorrectDateDay(TextBox b)
      {
          int day = 0;
  
          try
          {
              day = int.Parse(b.Text);
  
              if (day &lt; 0 || day &gt; 31)
              {
                  MessageBox.Show("Please enter a correct day.");
                  b.Text = string.Empty;
                  b.Focus();
              }
          }
          catch (FormatException)
          {
              MessageBox.Show("Please enter a number.", "Incorrect Input", MessageBoxButton.OK, MessageBoxImage.Warning);
              b.Text = string.Empty;
              b.Focus();
          }
          catch (Exception)
          {
              throw;
          }
      }
  </code></pre>
  
  <p>Now what I want it to do is check for correct input, and if that fails, set the focus back to whatever <code>TextBox</code> had an incorrect entry.</p>
  
  <p>It doesn't work though. After I enter a number outside the range (or letter), the <code>MessageBox</code> will show but the focus shifts to the next TextBox which is for entering the month.</p>
  
  <p>What am I doing wrong?</p>