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

Reading data from a log file as a separate application is writing to it

c++ perl osx file-io logging

I would like to monitor a log file that is being written to by an application. I want to process the file line by line as, or shortly after, it is written. I have not found a way of detecting that ...

Consolidating WCF Service Library config files when hosting in IIS

c# asp.net wcf iis .net-4.0

I have a number of WCF Service Libraries that each have their own App.config. I host these libraries in an ASP.Net project using serviceActivations / fileless. This ASP.Net project also has a

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.