How to prevent System.Windows.Forms.NumericUpDown from allowing to type non-digits when DecimalPlaces = 0?
I wish to make this WinForm control (NumericUpDown) enforce whole numbers conditionally depending on the type (whole vs floating) of the thing that I am working with.
If I set the DecimalPlaces = 0, it displays 0, and then increments/decrements that by 1 when I click up/down. However, if I enter 0.6, then it will display as 1, but will remain as 0.6. If I subsequently increment it, the underlying but not displayed value will be 1.6.
I am looking for a simple, idiomatic way to enforce what I want (hopefully it is clear what I want). If I have to intercept some sort of event, then I will, but I hope to just rely on some flag/setting that the NumericUpDown class already provides.
If your solution involves sub-classing NumericUpDown, then I will have to think about that. At this stage in the release I would prefer a well-documented hack to a clean change that might cause bugs elsewhere. I would like to have the option of not sub-classing the NumericUpDown.
Let me know if you have questions, thanks.
The underlying value is of type decimal. The DecimalPlaces only affects the number of digits displayed in the control. The easiest way to achieve what you want is to round the NumericUpDown.Value to an int.