none
WPF textbox to accept decimal numbers RRS feed

  • Question

  • I want textbox to accept the decimal numbers like 123.45 or 0.45

    I have added a code in PreviewtextInput event of textbox 

      foreach (char ch in e.Text)

                    if (!Char.IsDigit(ch))

                        e.Handled = true;

    But it allows only to accept the numbers and not decimals.

    How I can allow the decimals numbers as it is a textbox of currency(amount).

     

    Thanks

     

    Monday, May 24, 2010 2:34 PM

Answers

  • Hi Jegan,

    Agree with Smith, Regular Expressions can make it easier.

    Code snippets:

      private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
      {
       Regex regex = new Regex("^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$");
       e.Handled = !regex.IsMatch((sender as TextBox).Text.Insert((sender as TextBox).SelectionStart,e.Text));
      }
    

     

    Hope this helps.

    Sincerely,

    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, May 27, 2010 3:25 AM
    Moderator
  • Hi,

      This behavior is perfectly correct because the dot - "." standing alone does not have any meaning as a digit.

    following are valid digits: 1, 3.3, 23.124, 1., 23.0

    Following are invalid digit: . -->(i meant dot), .12

    so use the following code:

     

     

    foreach (char ch in e.Text)

     

     

    if (!(Char.IsDigit(ch) || ch.Equals('.')))

    {

    e.Handled =

     

    true;

    }

     


    - Jegan
    • Proposed as answer by Jegan Ravi Monday, May 24, 2010 5:30 PM
    • Marked as answer by Bob_BaoModerator Tuesday, June 1, 2010 1:07 AM
    Monday, May 24, 2010 5:30 PM

All replies

  • Why not do it in OnKeyDown?

    And you can check KeyEventArgs.Key to see if you should handle the event.

        protected override void OnKeyDown(System.Windows.Input.KeyEventArgs aKeyEventArgs)
    
        {
    
          if (this._AllowedKeys.Count > 0)
    
          {
    
            aKeyEventArgs.Handled = !this._AllowedKeys.Contains(aKeyEventArgs.Key);
    
          }
    
    
    
          base.OnKeyDown(aKeyEventArgs);
    
        }

    For decimals..

                    this._AllowedKeys.Add(System.Windows.Input.Key.Decimal);

                    this._AllowedKeys.Add(System.Windows.Input.Key.OemPeriod);

    Monday, May 24, 2010 4:35 PM
  • Hi,

      This behavior is perfectly correct because the dot - "." standing alone does not have any meaning as a digit.

    following are valid digits: 1, 3.3, 23.124, 1., 23.0

    Following are invalid digit: . -->(i meant dot), .12

    so use the following code:

     

     

    foreach (char ch in e.Text)

     

     

    if (!(Char.IsDigit(ch) || ch.Equals('.')))

    {

    e.Handled =

     

    true;

    }

     


    - Jegan
    • Proposed as answer by Jegan Ravi Monday, May 24, 2010 5:30 PM
    • Marked as answer by Bob_BaoModerator Tuesday, June 1, 2010 1:07 AM
    Monday, May 24, 2010 5:30 PM
  • Hi,

    you could use Decimal.TryParse(e.text, testvar) for this kind of check...


    http://wpfglue.wordpress.com
    Monday, May 24, 2010 5:40 PM
  • HI

    But how to restrict the number of decimal to one.

     

    foreach (char ch in e.Text)

     

     

     

    if (!(Char.IsDigit(ch) || ch.Equals('.')))

     

     

    true;

    }

    As it allows more than one decimal i.e dot(.)

    Thanks

    {

    e.Handled =

    Monday, May 24, 2010 6:19 PM
  • You may want to look into using RegularExpressions to validate your value.

    http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex%28VS.80%29.aspx

    Monday, May 24, 2010 6:26 PM
  • Hi Jegan,

    Agree with Smith, Regular Expressions can make it easier.

    Code snippets:

      private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
      {
       Regex regex = new Regex("^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$");
       e.Handled = !regex.IsMatch((sender as TextBox).Text.Insert((sender as TextBox).SelectionStart,e.Text));
      }
    

     

    Hope this helps.

    Sincerely,

    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, May 27, 2010 3:25 AM
    Moderator