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.

Answers


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.


Need Your Help

Ivy Retrieve with Classifiers

ivy

I have the following ivy.xml:

Shorten an url with '#' symbol via bit.ly API

api url escaping bit.ly

So, i have an url - http://somesite.com/#sometext. I need to shorten this url with the bit.ly API. The problem is, i cant properly escape the '#' symbol. I dont get any short url's if trying to short

Can catch() be written to convert from one object type to another?

c++ exception try-catch

Given this code, where A is in a 3rd-party library, and B is code I wrote: