title: How to restrict user to TextBox if value entered is incorrect
body: <p>In my program I have a <code>TextBox</code> who's value must be set in a specific integer range. If it is not within this range, it should warn the user and then highlight the incorrect text inside of the <code>TextBox</code> for re-editing (implying that the user must enter a value that is in the correct range before they are allowed to leave the <code>TextBox</code>). <strong>How would I change my code so that it performs these operations?</strong></p>
  
  <p>This is what I have so far. I am using a <code>TextChanged</code> event. This code warns the user about the restriction breach and refocuses (I would like to highlight the value instead) on the <code>TextBox</code>, but does not prevent the user from clicking out of it afterward:</p>
  
  <pre><code>int maxRevSpeed;
  
  //Max Rev Speed -- Text Changed
  private void maxRevSpeed_textChanged(object sender, RoutedEventArgs e)
  {
      if (maxRevSpeed_textBox.Text == "" || maxRevSpeed_textBox.Text == " ")
          maxRevSpeed = 0;
      else
      {
          maxRevSpeed = Convert.ToInt32(maxRevSpeed_textBox.Text);
  
          if (maxRevSpeed &lt;= 0 || maxRevSpeed &gt; 45)
          {
              MessageBox.Show("Reverse Sensor speed must be between 0 and 45 FPM", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
          }
  
          maxRevSpeed_textBox.Focus();
      }
  }
  </code></pre>
  
  <p>Please note that this question is a revisit of a <a href="https://stackoverflow.com/questions/19939872/wondering-about-textbox-methods">former question</a> of mine. I am aware that it may be "frowned upon" to take this approach to a <code>TextBox</code>, but regardless I would still like to figure out how to implement such a thing. Thank you.</p>
  
  <p><strong>Update 1:</strong></p>
  
  <p>After looking at everyone's suggestions I have updated my code:</p>
  
  <pre><code>//Max Rev Speed -- Text Changed
  private void maxRevSpeed_textChanged(object sender, RoutedEventArgs e)
  {
      if (maxRevSpeed_textBox.Text == "" || maxRevSpeed_textBox.Text == " ") //Is Empty or contains spaces
          maxRevSpeed = 0;
      else if (!Regex.IsMatch(maxRevSpeed_textBox.Text, @"^[\p{N}]+$")) //Contains characters
          maxRevSpeed = 0;
      else
          maxRevSpeed = Convert.ToInt32(maxRevSpeed_textBox.Text);
  }
  
  //Max Rev Speed -- Lost Focus
  private void maxRevSpeed_LostFocus(object sender, RoutedEventArgs e)
  {
      if (maxRevSpeed &lt;= 0 || maxRevSpeed &gt; 45)
      {
          MessageBox.Show("Reverse Sensor speed must be between 0 and 45 FPM", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
  
          //Supposed to highlight incorrect text -- DOES NOT WORK
          maxRevSpeed_textBox.SelectionStart = 0;
          maxRevSpeed_textBox.SelectionLength = maxRevSpeed_textBox.Text.Length;
      }
  }
  </code></pre>
  
  <p>The <code>integer</code> representing the text in the <code>textBox</code> is now dealt with in the <code>textChanged</code> event. The <code>LostFocus</code> event handles the warning and the re-selection of the incorrect text value. However, the highlight text method works when it is in the <code>textChanged</code> event, but not in it's current location. Why is that, and how can I fix it?</p>