1

How can I have a WPF numberic textbox with two decimal point, for example:

It will start with 0.00, when user key in 1, the value will be 0.01, next when user user key in 2, the value will be 0.21.

When user key in 5003, the value is 30.05.

Thnak you.

1
  • Is there any open source solution? Commented Sep 4, 2012 at 3:27

2 Answers 2

4

Sure you can always implemented one as @JesseJames already suggested. But, I'll suggest you to better use an existing one, I believe the Extended WPF Toolkit is what you need, precisely the IntegerUpDown (you can specify the mask you need, it comes with 5):

<xctk:IntegerUpDown FormatString="N0" Value="1" Increment="1" Maximum="100"/>
Sign up to request clarification or add additional context in comments.

Comments

-1

You can implement it in KeyDown event handler. Set event argument property e.Handle = true and calculate output number.

Don't write like me, it's just example :))

public partial class MainWindow : Window
{
    private StringBuilder sb = new StringBuilder();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        e.Handled = true;
        switch (e.Key)
        {
            case Key.D0: { if (sb.Length == 2) sb.Insert(0, ','); sb.Insert(0, 0); break; }
            case Key.D1: { if (sb.Length == 2) sb.Insert(0, ','); sb.Insert(0, 1); break; }
            case Key.D2: { if (sb.Length == 2) sb.Insert(0, ','); sb.Insert(0, 2); break; }
            case Key.D3: { if (sb.Length == 2) sb.Insert(0, ','); sb.Insert(0, 3); break; }
            case Key.D4: { if (sb.Length == 2) sb.Insert(0, ','); sb.Insert(0, 4); break; }
            case Key.D5: { if (sb.Length == 2) sb.Insert(0, ','); sb.Insert(0, 5); break; }
            case Key.D6: { if (sb.Length == 2) sb.Insert(0, ','); sb.Insert(0, 6); break; }
            case Key.D7: { if (sb.Length == 2) sb.Insert(0, ','); sb.Insert(0, 7); break; }
            case Key.D8: { if (sb.Length == 2) sb.Insert(0, ','); sb.Insert(0, 8); break; }
            case Key.D9: { if (sb.Length == 2) sb.Insert(0, ','); sb.Insert(0, 9); break; }
        }
        textBox1.Text = sb.ToString();
    }
}

In this example you also need to handle "Backspace" hit to clear StringBuilder. To get the value, use parser: double result = double.Parse(sb.ToString()); + Handle NumPad numbers!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.