Reputation: 8499
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.
Upvotes: 1
Views: 5639
Reputation: 15557
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"/>
Upvotes: 4
Reputation: 5083
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!
Upvotes: -1