BLOG.CSHARPHELPER.COM: Use a NumericUpDown control to let the user enter decimal values in C#
Use a NumericUpDown control to let the user enter decimal values in C#
Most developers know that the NumericUpDown control lets a user select a number but few know that it can handle decimal numbers in addition to integers.
The control's DecimalPlaces property determines the number of digits after the decimal the control allows. The Increment property determines the amount the value changes when the user clicks the up or down arrows. By default Increment is 1. If you set DecimalPlaces greater than 0, you may want to set Increment to a value less than 0 to make it easier for the user to select values. You may also want to change Minimum and Maximum to restrict the user to a reasonable range.
Note that this control can be awkward if the user must select a value in a large range with many digits after the decimal point. For example, if Minimum = 0, Maximum = 1000, DecimalPlaces = 3, and Increment = 0.001, the user really can't select the value 987.654 by clicking the up and down arrows.
This example lets the user select a value between 0 and 1 with DecimalPlaces = 2. The program then draws a ellipse with width and height that are equal to the selected number times the form's width and height.
The program executes the following code when it starts to prepare the NumericUpDown control named nudScale.
Comments