NumericUpDown
control or up-down control or spin box is very useful control, allows to select the value from the range of values. It allows the users to increment or decrement the value within the given range by using Keyboard Up & Down arrow keys or by using the Mouse.
Properties
Minimum
& Maximum
properties are used to set the boundary values of the NumericUpDown
control. When set these properties, the control allows only the values within the given range. When we set the value which is not within the given range, the control automatically resets the value to either it’s Minimum
or Maximum
value.
Value
is the property through which we can get or set the control’s value, which is in the range given through it’s Minimum
& Maximum
properties. That means, the value of the Value
property should be greater than or equals to it’s Minimum
value and less than or equals to it’s Maximum
value.
Note that,
Value
property is ofdecimal
orSystem.Decimal
type. Explicit casts are required when assigning the value to other variables. For example, assigning this value to an integer type variable without explicit cast will through an error message, “Compiler Error CS0266“.
The Value
of the control will be changed, when user clicks on up or down button of the control; or by pressing the Keyboard Up or Down arrow keys. How much value will be changed? This will depends on the value we set in control’s Increment
property. As the Value
property is of decimal
type, Increment
property is also of decimal
type.
For example, when Increment
value is set to 1, the Value
will increment or decrement by 1 when we click on the up or down buttons of the control (or by pressing Keyboard’s Up or Down arrow keys).
DecimalPlaces
property is useful to set the number of decimal places to display the decimal value.
When set Hexadecimal
property to True, the control will display it’s value in hexadecimal format.
A thousand separator will be shown in the value, when set the ThosuandSeparator
property to True.
Methods
UpButton()
& DownButton()
methods are useful to increment and decrement the value of the NumericUpDown
control or spin box (up-down control). If you want to update the value of the control through the program statements, you can use these methods.
Events
ValueChanged
is the most useful event, to use to place our code to execute when NumericUpDown
control’s value is changed.
NumericUpDown control – working example
Let’s put all together, what we discussed and here is the complete working example;
Upon successful execution of the program, the Application Window looks like below;
(Raju)