0

I am using a NumericUpDown in WinForms and I'm wondering if it is possible to restrict the number of digits. So my max value is set to 9999.99 but you can enter values larger than this. you shouldn't be able to enter more than 4 digits to the left of the decimal and you shouldn't be able to enter more than 2 digits to the right of the decimal. There is another component called DoubleTextBox which uses the NumberFormatInfo.NumberDecimalDigits property to achieve this, but i was wondering if there is something similar for NumericUpDown. I've tried using the KeyDown event to stop the keyboard press but it doesn't work as perfectly. Any help would be great !

7
  • Are you looking for NumericUpDown.DecimalPlaces? Commented Aug 22, 2021 at 3:46
  • You can use the Keyboard to write a value that is > Maximum, but that value is not retained. As soon as you press enter or leave the Control, the maximum value is applied. - You could replace it with a MaskedTextBox, eventually, but is there a problem if a User tries to write a value that is just reset to the interval you have specified when the Control is validated? Commented Aug 22, 2021 at 5:49
  • @Steeeve NumericUpDown.DecimalPlaces only sets the number of decimal places Commented Aug 22, 2021 at 6:18
  • @Jimi Our testers are fussy and wanted to try and make it work by using the NumericUpDown component. So i thought i would try ask if it was possible to replicate the same behaviour you can have for a DoubleTextBox. MaskedTextBox is also similar to DoubleTextBox but both these components dont have the up and down arrows unfortunately Commented Aug 22, 2021 at 6:23
  • Well, you'd have to use a NativeWindow set to the handle of the child Edit Control of your NumericUpDown and handle the input directly (you cannot test the Text property of the NUD, because it's updated after the Edit notifies the change: so, too late). You can then handle WM_KEYDOWN and accept the input only if it doesn't violate the rules you have set. - The debugger(s) guy(s) will hate you. Commented Aug 22, 2021 at 7:03

0

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.