Home

Windows Control: Numeric Up-Down

 

Introduction to the Numeric Up Down Control

 

Description

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. To use it, from the Common Controls section of the Toolbox, you can click the NumericUpDown button NumericUpDown and click the form or the container that will host it. This control is based on the NumericUpDown class that you can use to dynamically create the control. Here is an example:

Imports System.Drawing
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Private nudCounter As NumericUpDown


        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()

            nudCounter = New NumericUpDown()
            nudCounter.Location = New Point(12, 12)

            Controls.Add(nudCounter)

        End Sub

    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter

        Application.Run(frmStart)

        Return 0
    End Function

End Module

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:

Public Sub InitializeComponent()

            nudCounter = New NumericUpDown()
            nudCounter.Location = New Point(12, 12)

            nudCounter.UpDownAlign = LeftRightAlignment.Left

            Controls.Add(nudCounter)

End Sub

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, whose 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 an 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 using the Properties window. 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 System.Decimal types. These numbers range from a minimum controlled by the Minimum property to a maximum value controlled by the Maximum property. By default, a freshly added numeric up-down control on a form has its Minimum value set to 0 and its Maximum value set to 100. You can change the values at design time in the Properties window or programmatically as follows:

Public Sub InitializeComponent()

            nudCounter = New NumericUpDown()
            nudCounter.Location = New Point(12, 12)

            nudCounter.Minimum = 42.48D
            nudCounter.Maximum = 3822046.06D

            Controls.Add(nudCounter)

End Sub

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. 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 whose 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:

Public Sub InitializeComponent()

            nudCounter = New NumericUpDown()
            nudCounter.Location = New Point(12, 12)

            nudCounter.Minimum = 42.48D
            nudCounter.Maximum = 3822046.06D

            nudCounter.Hexadecimal = True

            Controls.Add(nudCounter)

End Sub

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 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 whose default value is False. If you want to display a symbol between thousands, set this property to True. Here is an example:

Public Sub InitializeComponent()

            nudCounter = New NumericUpDown()
            nudCounter.Location = New Point(12, 12)

            nudCounter.Minimum = 42.48D
            nudCounter.Maximum = 3822046.06D

            nudCounter.ThousandsSeparator = True

            Controls.Add(nudCounter)

End Sub

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. 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:

Public Sub InitializeComponent()

            nudCounter = New NumericUpDown()
            nudCounter.Location = New Point(12, 12)

            nudCounter.Minimum = 42.48D
            nudCounter.Maximum = 3822046.06D

            nudCounter.Increment = 125.82D

            nudCounter.ThousandsSeparator = True

            Controls.Add(nudCounter)

End Sub

 

 

Home Copyright © 2008-2016, FunctionX, Inc.