1

I have a requirement to make a decimal textbox (Money textbox) which:

  1. only allows numbers 0-9 (allow upper numpad keys 0-9 and right numpad keys 0-9);
  2. allows only one dot which don't appear on start.

Valid:

  • 0.5
  • 1
  • 1.5000

Invalid:

  • .5
  • 5.500.55

Edit

my code is :

 private void floatTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        e.Handled = !TextBoxValidation.AreAllValidDecimalChars(e.Text);
    }


  public static class TextBoxValidation
{
    public static bool AreAllValidDecimalChars(string str)
    {
        bool ret = false;
        int x = 0;
        foreach (char c in str)
        {
            x = (int)c;
        }
        if (x >= 48 && x <= 57 || x == 46)
        {
            ret = true;
        }
        return ret;
    }
}
Mussammil
  • 864
  • 4
  • 16
  • 38

2 Answers2

2

If you want to allow copy and pasting as well you cannot do it with keyboard events. A TextBox has a TextChanged event which allows you to handle this event appropirately. If you want to block any input that is not a number or dot you could handle it like this:

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    //get the textbox that fired the event
    var textBox = sender as TextBox;
    if (textBox == null) return;

    var text = textBox.Text;
    var output = new StringBuilder();
    //use this boolean to determine if the dot already exists
    //in the text so far.
    var dotEncountered = false;
    //loop through all of the text
    for (int i = 0; i < text.Length; i++)
    {
        var c = text[i];
        if (char.IsDigit(c))
        {
            //append any digit.
            output.Append(c);
        }
        else if (!dotEncountered && c == '.') 
        {
            //append the first dot encountered
            output.Append(c);
            dotEncountered = true;
        }
    }
    var newText = output.ToString();
    textBox.Text = newText;
    //set the caret to the end of text
    textBox.CaretIndex = newText.Length;
}
Bas
  • 26,772
  • 8
  • 53
  • 86
  • ,thanks for ur response ,this will not solve my requirement 2 (dot never come as first character eg: .5 is invalid instead 0.5 is valid) how to accomplish this too – Mussammil Dec 04 '13 at 04:34
-1

You can achieve your goal by simply implementing two event handlers on a TextBox:

TextCompositionEventHandler textBox_PreviewTextInput = 
    new TextCompositionEventHandler((s, e) => e.Handled = !e.Text.All(
    c => Char.IsNumber(c) || c == '.'));
KeyEventHandler textBox_PreviewKeyDown = new KeyEventHandler(
    (s, e) => e.Handled = e.Key == Key.Space);

textBox.PreviewTextInput += textBox_PreviewTextInput;
textBox.PreviewKeyDown += textBox_PreviewKeyDown;
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • 1
    Try pasting 'Hello World!' into the textbox. – Bas Dec 03 '13 at 13:08
  • +1 Thanks for pointing that out @BasBrekelmans... I've been using this for years and never noticed that. I would have fixed that using the `TextChanged` event, but not wanting to copy your answer, I'll just keep the -1. – Sheridan Dec 03 '13 at 13:28