I am doing an Financial Winforms application and am having some trouble with the controls.
My customer needs to insert decimal values all over the place (Prices, Discounts etc) and I'd like to avoid some of the repeating validation.
So I immediately tried the MaskedTextBox that would fit my needs (with a Mask like "€ 00000.00"), if it weren't for the focus and the length of the mask.
I can't predict how big the numbers are my customer is going to enter into the app.
I also can't expect him to start everything with 00 to get to the comma. Everything should be keyboard-friendly.
Am I missing something or is there simply no way (beyond writing a custom control) to achieve this with the standard Windows Forms controls?
I don't think you need a custom control, just write a decimal validating method for the validating event and use that for all the places you need to validate. Don't forget to include the NumberFormatInfo, it will deal with commas and numebr signs.
Another approach is to block what you don't want, and format when your done with it.
You will need a custom control. Just trap the Validating event on the control and check if the string input can be parsed as a decimal.
You only need to let numbers and decimal symbols through, and avoid a double decimal symbol. As an extra, this automatically adds a 0 before a starting decimal number.
MSDN: User Input Validation in Windows Forms
This two overriden methods did it for me (disclaimer: this code is not in production yet. You may need to modify)