Implements the basic functionality required by a spin box (also known as an up-down control).
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
A spin box consists of a text box and a small vertical scroll bar, commonly referred to as a spin button control. The class links the two controls and allows the user to change the display in the text box by clicking the up or down buttons or by entering the appropriate type of value directly into the text box. Use the spin box in cases where you want to limit the list of values a user can select, similar to a list box or combo box. Depending upon the type of list you want to display, the advantage to using a spin box is that it allows you to quickly set a range of valid values, rather than adding items one at a time. Implementing a spin box requires less data validation than a text box, as you can limit the data type when you derive a class from . An example of this is the NumericUpDown class, which limits the values to the numeric type and uses a Minimum and Maximum property to validate the data.
To allow the user to use the arrow keys to change the contents of a spin box, set the InterceptArrowKeys property to true. To restrict the user to values you specify, set the ReadOnly property to true. To control the alignment of text in the spin box, set the TextAlign property. To set the alignment of the up and down buttons in relation to the text box portion of the control, set the UpDownAlign property to either Left or Right.
The UpButton and DownButton methods, when overridden, handle the clicking of the up or down buttons. When overridden, the ValidateEditText and UpdateEditText methods validate the value (either selected or entered) and update the text displayed in the spin box. If the value fails validation, use the Select method to select the text that is not valid. This allows the user to quickly correct the text by simply typing in a new value without having to manually select or delete the existing text.Notes to Inheritors:
The following code example uses the derived class NumericUpDown and sets some of its properties derived from . This code requires that you have a NumericUpDown control named numericUpDown1, two ComboBox controls named comboBox1 and comboBox2, and three CheckBox controls named checkBox1, checkBox2, and checkBox2 created on a form. Add the following items to comboBox1: None, Fixed3D, and FixedSingle. Add the following items to comboBox2: Left, Right, and Center.
The code allows you to change the property values at run time and see how each affects the appearance and behavior of the spin box.
Private Sub comboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) ' Set the BorderStyle property. Select Case comboBox1.Text Case "Fixed3D" numericUpDown1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Case "None" numericUpDown1.BorderStyle = System.Windows.Forms.BorderStyle.None Case "FixedSingle" numericUpDown1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle End Select End Sub Private Sub comboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) ' Set the TextAlign property. Select Case comboBox2.Text Case "Right" numericUpDown1.TextAlign = HorizontalAlignment.Right Case "Left" numericUpDown1.TextAlign = HorizontalAlignment.Left Case "Center" numericUpDown1.TextAlign = HorizontalAlignment.Center End Select End Sub Private Sub checkBox1_Click(sender As Object, e As EventArgs) ' Evaluate and toggle the ReadOnly property. If numericUpDown1.ReadOnly Then numericUpDown1.ReadOnly = False Else numericUpDown1.ReadOnly = True End If End Sub Private Sub checkBox2_Click(sender As Object, e As EventArgs) ' Evaluate and toggle the InterceptArrowKeys property. If numericUpDown1.InterceptArrowKeys Then numericUpDown1.InterceptArrowKeys = False Else numericUpDown1.InterceptArrowKeys = True End If End Sub Private Sub checkBox3_Click(sender As Object, e As EventArgs) ' Evaluate and toggle the UpDownAlign property. If numericUpDown1.UpDownAlign = LeftRightAlignment.Left Then numericUpDown1.UpDownAlign = LeftRightAlignment.Right Else numericUpDown1.UpDownAlign = LeftRightAlignment.Left End If End Sub
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.