Home

Windows Controls: The Numeric Up-Down Control

 

Fundamentals of the Numeric Up Down Control

 

Introduction

A spin button, also called a spin box, also called an up/down control, is a Windows control equipped with two opposite arrow buttons The UpDown Control. The user clicks one of the arrow buttons at one time to increase or decrease the current value of the control. The value held by the control is also called its position. 

The values of an up/down control range from a minimum to a maximum. When the up arrow button is clicked, the value of the control increases. If the user clicks and holds the mouse on the up pointing arrow button, the value of the control keeps increasing until it reaches its maximum and then stops. The opposite behavior applies when the user clicks or holds the mouse on the down-pointing arrow button.

Creating an Up-Down Control

The .NET Framework provides two types of spin buttons. The immediate parent of the up/down control is the UpDownBase class.  This class provides the characteristics and behaviors common to both types of up/down controls.

As it name implies, the numeric up-down control is made to display numeric values. The numeric up-down button is based on a class mamed NumericUpDown. Here is an example of creating an up-down control:

open System
open System.Drawing
open System.Windows.Forms

let exercise : Form = new  System.Windows.Forms.Form()
exercise.ClientSize <- new Size(145, 45)
exercise.StartPosition <- FormStartPosition.CenterScreen

let nudCounter = new NumericUpDown()
nudCounter.Location <- new Point(12, 12)
exercise.Controls.Add nudCounter

Application.Run exercise

This would produce:

Numeric Up-Down

Characteristics of the Numeric Up-Down Control

 

The Up-Down Alignment

In traditional Win32 programming, the spin button does not have a means of displaying its value. This means that you usually have to accompany it with another control such as a text box. You also have to decide whether to place the text box to the left or the right side of the spin button control. Although the .NET Framework's up-down controls don't have this limitation, you still have to decide whether to position the arrow buttons on the left or the right side of the text box part. This property is controlled by the UpDownAlign Boolean property whose default value is Right, which places the arrow buttons on the right side. If you want the buttons to be positioned on the left, set this property to Left. The values of this property are managed through the LeftRightAlignment enumeration of the UpDownBase parent class. Here is an example of aligning the buttons to the left:

open System
open System.Drawing
open System.Windows.Forms

let exercise : Form = new  System.Windows.Forms.Form()
exercise.ClientSize <- new Size(145, 45)
exercise.StartPosition <- FormStartPosition.CenterScreen

let nudCounter = new NumericUpDown()
nudCounter.Location <- new Point(12, 12)

nudCounter.UpDownAlign <- LeftRightAlignment.Left

exercise.Controls.Add nudCounter

Application.Run exercise

This would produce:

The UpDown control with the buttons aligned to the left of the text box

Intercepting the Arrow Keys

When a spin button control comes up, to use it, the user can click one of the up or down-pointing buttons, which causes the value to change. The user can also press the up or down arrow keys to change the value. The ability to use the keyboard is controlled by the InterceptArrowKeys Boolean property:

member InterceptArrowKeys : bool with get, set

The default value is true, which means the user is allowed to use the keyboard to change the value of the control. If for some (strange) reason you want to prevent the user from changing the value using the keyboard, set this property to false. If this property is set to true, remember that the user can use the keyboard only after giving focus to the control, which is usually done by pressing Tab a few times until the control receives focus. Another way the user can change the value of the control is to manually edit the value of the text box part of the control.

When the value of a spin button has been changed, the control fires a ValueChanged() event. This event simply uses an EventArgs class as argument. If the user decides to manually edit the value of the control by typing a number in the text box part of the control, the spin button fires a TextChanged() event.

The Minimum and the Maximum Values

After adding the up-down control to a container such as a form, you change its characteristics. Probably the most important piece of information you would need from a spin button is the value it is holding at a particular time. As mentioned already, the spin button navigates from a minimum to a maximum value. The values of the control can be natural or decimal numbers. They are actually defined as decimal types. These numbers range from a minimum controlled by the Minimum property to a maximum value controlled by the Maximum property:

member Maximum : decimal with get, set
member Minimum : decimal with get, set

By default, a numeric up-down control has its Minimum value set to 0 and its Maximum value set to 100. You can change the values as follows:

open System
open System.Drawing
open System.Windows.Forms

let exercise : Form = new  System.Windows.Forms.Form()
exercise.ClientSize <- new Size(145, 45)
exercise.StartPosition <- FormStartPosition.CenterScreen

let nudCounter = new NumericUpDown()
nudCounter.Minimum <- 42.48M
nudCounter.Maximum <- 3822046.06M
nudCounter.Location <- new Point(12, 12)

nudCounter.UpDownAlign <- LeftRightAlignment.Left

exercise.Controls.Add nudCounter

Application.Run exercise

If you use large numbers in the thousands, they may become difficult to read:

The Value of the Control

When, or while, a spin button is being used, its text box displays a value: this is the Value property:

member Value : decimal with get, set

You can use this property to specify what value the control would use at startup. It can be an integer or a decimal number but it must be between the Minimum and the Maximum values.

 
   
 

The Type of Value

By default, the numeric up-down displays natural numbers. Alternatively, you can make the spin button display hexadecimal numbers. The type of numbers displayed is controlled by the Boolean Hexadecimal property:

member Hexadecimal : bool with get, set

The default value is False, which means that it is primarily meant to display natural numbers. If you prefer the control to display hexadecimal numbers, set this property to True. Here is an example:

open System
open System.Drawing
open System.Windows.Forms

let exercise : Form = new  System.Windows.Forms.Form()
exercise.ClientSize <- new Size(145, 45)
exercise.StartPosition <- FormStartPosition.CenterScreen

let nudCounter = new NumericUpDown()
nudCounter.Minimum <- 42.48M
nudCounter.Maximum <- 3822046.06M
nudCounter.Hexadecimal <- true
nudCounter.Location <- new Point(12, 12)

nudCounter.UpDownAlign <- LeftRightAlignment.Left

exercise.Controls.Add nudCounter

Application.Run exercise

This would produce:

Displaying Decimal Numbers

By default, the numeric up-down control is made to display only natural numbers. If you want it to display decimal numbers, use the DecimalPlaces property:

member DecimalPlaces : int with get, set

To specify the number of decimal places on the right side of the decimal separator which, in US English, is the period.

The Thousand Separator

If you want to make the control's numeric values easier to read, you can display a symbol to separate the thousands. This characteristic can be set using the Boolean ThousandsSeparator property:

member ThousandsSeparator : bool with get, set

The default value is False. If you want to display a symbol between thousands, set this property to True. Here is an example:

open System
open System.Drawing
open System.Windows.Forms

let exercise : Form = new  System.Windows.Forms.Form()
exercise.ClientSize <- new Size(145, 45)
exercise.StartPosition <- FormStartPosition.CenterScreen

let nudCounter = new NumericUpDown()
nudCounter.Minimum <- 42.48M
nudCounter.Maximum <- 3822046.06M

nudCounter.ThousandsSeparator <- true

nudCounter.Location <- new Point(12, 12)

nudCounter.UpDownAlign <- LeftRightAlignment.Left

exercise.Controls.Add nudCounter

Application.Run exercise

This causes the control to check the value used as the thousands separator in the Control Panel of the computer that is using the application. The thousands separator for US English is the comma ",". Here is an example:

The NumericUpDown control displaying the thousands separator

The Incrementing Value

When using the spin button, the user clicks one of the arrows of the control to increase or decrease the value. By default, the value increases or decreases by 1. If you want the value to augment by more than 1, set the desired value using the Increment property:

member Increment : decimal with get, set

The value of this property can be a natural or a decimal value (it is defined as System.Decimal). To set the Increment value programmatically, you can use code as follows:

open System
open System.Drawing
open System.Windows.Forms

let exercise : Form = new  System.Windows.Forms.Form()
exercise.ClientSize <- new Size(145, 45)
exercise.StartPosition <- FormStartPosition.CenterScreen

let nudCounter = new NumericUpDown()
nudCounter.Minimum <- 42.48M
nudCounter.Maximum <- 3822046.06M
nudCounter.Increment <- 125.82M
nudCounter.ThousandsSeparator <- true
nudCounter.Location <- new Point(12, 12)

nudCounter.UpDownAlign <- LeftRightAlignment.Left

exercise.Controls.Add nudCounter

Application.Run exercise
 
 
   
 

Home Copyright © 2015 FunctionX Home