title: Number only TextBox (WPF)
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1268552/how-do-i-get-a-textbox-to-only-accept-numeric-input-in-wpf">How do I get a TextBox to only accept numeric input in WPF?</a>  </p>
  </blockquote>
  
  
  
  <p>The following codes are not correct :</p>
  
  <pre><code> private  void txtCode_PreviewTextInput(object sender, TextCompositionEventArgs e)
      {
          e.Handled = !AreAllValidNumericChars(e.Text);
  
  
  
      }
      private bool AreAllValidNumericChars(string str)
      {
          foreach (char c in str)
          {
              if (!Char.IsNumber(c)) return false;
          }
  
          return true;
      }
  </code></pre>
  
  <p><strong>and</strong></p>
  
  <pre><code> private  void txtCode_PreviewTextInput(object sender, TextCompositionEventArgs e)
      {
          e.Handled = !AreAllValidNumericChars(e.Text);
  
      }
      bool AreAllValidNumericChars(string str)
      {
          bool ret = true;
          if (str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator |
              str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyGroupSeparator |
              str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencySymbol |
              str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeSign |
              str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeInfinitySymbol |
              str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator |
              str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberGroupSeparator |
              str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentDecimalSeparator |
              str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentGroupSeparator |
              str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentSymbol |
              str == System.Globalization.NumberFormatInfo.CurrentInfo.PerMilleSymbol |
              str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveInfinitySymbol |
              str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveSign)
              return ret;
  
          int l = str.Length;
          for (int i = 0; i &lt; l; i++)
          {
              char ch = str[i];
              ret &amp;= Char.IsDigit(ch);
          }
  
          return ret;
      }
  </code></pre>
  
  <p><strong>and</strong></p>
  
  <pre><code>     private override void txtCode_PreviewTextInput(object sender, TextCompositionEventArgs e)
      {
          char c = e.Text.ToCharArray().First();
          e.Handled = !(char.IsNumber(c) || char.IsControl(c));
  
      }
  </code></pre>
  
  <p>and .....</p>
  
  <p>The problem : Ctrl+V   Copy/Paste</p>
  
  <p>What is the correct code?</p>