Wednesday, February 23, 2011

Safely set NumericUpDown control value

Very quick post about an extension method on the NumericUpDown control I once put to great use. It's small, quick, simple, needs the System.Windows.Forms and System namespaces, but can be of great use. Enjoy:

public static void SafeValue(this NumericUpDown c, decimal value)
   c.Value = Math.Max(c.Minimum, Math.Min(value, c.Maximum));

The principle will no doubt work on similar controls for WPF/Silverlight or other controls - like sliders - that at least have a value that is constrained by properties and might give exceptions if you try to set the value to an illegal value.

1 comment:

  1. Hi,
    Your article are really awesome.actually i was in search for some good articles on NumericUpDown control in C#.Net and finally I got one.
    The most important is the simplicity which will be very helpful for the beginners. Check this url too its also having nice post with wonderful explanation.