Export (0) Print
Expand All

NumericUpDown Class

Represents a Windows up-down control that displays numeric values.

For a list of all members of this type, see NumericUpDown Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.Control
            System.Windows.Forms.ScrollableControl
               System.Windows.Forms.ContainerControl
                  System.Windows.Forms.UpDownBase
                     System.Windows.Forms.NumericUpDown

[Visual Basic]
Public Class NumericUpDown
   Inherits UpDownBase
   Implements ISupportInitialize
[C#]
public class NumericUpDown : UpDownBase, ISupportInitialize
[C++]
public __gc class NumericUpDown : public UpDownBase,
   ISupportInitialize
[JScript]
public class NumericUpDown extends UpDownBase implements
   ISupportInitialize

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

A NumericUpDown control contains a single numeric value that can be incremented or decremented by clicking the up or down buttons of the control. The user can also enter in a value, unless the ReadOnly property is set to true.

The numeric display can be formatted by setting the DecimalPlaces, Hexadecimal, or ThousandsSeparator properties. To display hexadecimal values in the control, set the Hexadecimal property to true. To display a thousands separator in decimal numbers when appropriate, set the ThousandsSeparator property to true. To specify the number of digits displayed after the decimal symbol, set the DecimalPlaces property to the number of decimal places to display.

To specify the allowable range of values for the control, set the Minimum and Maximum properties. Set the Increment value to specify the value to be incremented or decremented to the Value property when the user clicks the up or down arrow buttons.

When the UpButton or DownButton methods are called, either in code or by the click of the up or down buttons, the new value is validated and the control is updated with the new value in the appropriate format. Specifically, if UserEdit is set to true, ParseEditText is called prior to validating or updating the value. The value is then verified to be between the Minimum and Maximum values and the UpdateEditText method is called.

Example

The following example creates and initializes a NumericUpDown control, sets some of its common properties, and allows the user to change some of these properties at run time. This code assumes three CheckBox controls have been placed on a form and handlers for their Click events have been instantiated. The DecimalPlaces, ThousandsSeparator and Hexadecimal properties are set on the Click event of each check box.

[Visual Basic] 
Public Sub InstantiateMyNumericUpDown()
    ' Create and initialize a NumericUpDown control.
    numericUpDown1 = New NumericUpDown()
    
    ' Dock the control to the top of the form.
    numericUpDown1.Dock = System.Windows.Forms.DockStyle.Top
    
    ' Set the Minimum, Maximum, and initial Value.
    numericUpDown1.Value = 5
    numericUpDown1.Maximum = 2500
    numericUpDown1.Minimum = - 100
    
    ' Add the NumericUpDown to the Form.
    Controls.Add(numericUpDown1)
End Sub    

' Check box to toggle decimal places to be displayed.
Private Sub checkBox1_Click(sender As Object, e As EventArgs)
    ' If DecimalPlaces is greater than 0, set them to 0 and round the
    ' current Value; otherwise, set DecimalPlaces to 2 and change the
    ' Increment to 0.25. 
    If numericUpDown1.DecimalPlaces > 0 Then
        numericUpDown1.DecimalPlaces = 0
        numericUpDown1.Value = Decimal.Round(numericUpDown1.Value, 0)
    Else
        numericUpDown1.DecimalPlaces = 2
        numericUpDown1.Increment = 0.25D
    End If
End Sub    

' Check box to toggle thousands separators to be displayed.
Private Sub checkBox2_Click(sender As Object, e As EventArgs)
    ' If ThousandsSeparator is true, set it to false;
    ' otherwise, set it to true. 
    If numericUpDown1.ThousandsSeparator Then
        numericUpDown1.ThousandsSeparator = False
    Else
        numericUpDown1.ThousandsSeparator = True
    End If
End Sub    

' Check box to toggle hexadecimal to be displayed.
Private Sub checkBox3_Click(sender As Object, e As EventArgs)
    ' If Hexadecimal is true, set it to false;
    ' otherwise, set it to true. 
    If numericUpDown1.Hexadecimal Then
        numericUpDown1.Hexadecimal = False
    Else
        numericUpDown1.Hexadecimal = True
    End If
End Sub


[C#] 
public void InstantiateMyNumericUpDown()
{
   // Create and initialize a NumericUpDown control.
   numericUpDown1 = new NumericUpDown();

   // Dock the control to the top of the form.
   numericUpDown1.Dock = System.Windows.Forms.DockStyle.Top;

   // Set the Minimum, Maximum, and initial Value.
   numericUpDown1.Value = 5;
   numericUpDown1.Maximum = 2500;
   numericUpDown1.Minimum = -100;
   
   // Add the NumericUpDown to the Form.
   Controls.Add(numericUpDown1);
}

// Check box to toggle decimal places to be displayed.
private void checkBox1_Click(Object sender,
                             EventArgs e)
{
   /* If DecimalPlaces is greater than 0, set them to 0 and round the 
      current Value; otherwise, set DecimalPlaces to 2 and change the 
      Increment to 0.25. */
   if (numericUpDown1.DecimalPlaces > 0)
   {
      numericUpDown1.DecimalPlaces = 0;
      numericUpDown1.Value = Decimal.Round(numericUpDown1.Value, 0);
   }
   else
   {
      numericUpDown1.DecimalPlaces = 2;
      numericUpDown1.Increment = 0.25M;
   }
}

// Check box to toggle thousands separators to be displayed.
private void checkBox2_Click(Object sender,
                             EventArgs e)
{   
   /* If ThousandsSeparator is true, set it to false; 
      otherwise, set it to true. */
   if (numericUpDown1.ThousandsSeparator)
   {
      numericUpDown1.ThousandsSeparator = false;
   }
   else
   {
      numericUpDown1.ThousandsSeparator = true;
   }
}

// Check box to toggle hexadecimal to be displayed.
private void checkBox3_Click(Object sender, 
                             EventArgs e)
{
   /* If Hexadecimal is true, set it to false; 
      otherwise, set it to true. */    
   if (numericUpDown1.Hexadecimal)
   {
      numericUpDown1.Hexadecimal = false;
   }
   else
   {
      numericUpDown1.Hexadecimal = true;
   }
}


[C++] 
public:
    void InstantiateMyNumericUpDown() {
        // Create and initialize a NumericUpDown control.
        numericUpDown1 = new NumericUpDown();
 
        // Dock the control to the top of the form.
        numericUpDown1->Dock = System::Windows::Forms::DockStyle::Top;
     
        // Set the Minimum, Maximum, and initial Value.
        numericUpDown1->Value = 5;
        numericUpDown1->Maximum = 2500;
        numericUpDown1->Minimum = -100;
        
        // Add the NumericUpDown to the Form.
        Controls->Add(numericUpDown1);
    };
 
    // Check box to toggle decimal places to be displayed.
private:
    void checkBox1_Click(Object *sender, EventArgs *e) {
        /* If DecimalPlaces is greater than 0, set them to 0 and round the 
        current Value; otherwise, set DecimalPlaces to 2 and change the 
        Increment to 0.25. */
        if (numericUpDown1->DecimalPlaces > 0) {
            numericUpDown1->DecimalPlaces = 0;
            numericUpDown1->Value = Decimal::Round(numericUpDown1->Value, 0);
        } else {
            numericUpDown1->DecimalPlaces = 2;
            numericUpDown1->Increment = 0.25;
        }
    };
 
    // Check box to toggle thousands separators to be displayed.
    void checkBox2_Click(Object *sender, EventArgs *e) {   
        /* If ThousandsSeparator is true, set it to false; 
        otherwise, set it to true. */
        if (numericUpDown1->ThousandsSeparator) {
            numericUpDown1->ThousandsSeparator = false;
        } else {
            numericUpDown1->ThousandsSeparator = true;
        }
    };
 
    // Check box to toggle hexadecimal to be displayed.
    void checkBox3_Click(Object *sender, EventArgs *e) {
        /* If Hexadecimal is true, set it to false; 
        otherwise, set it to true. */    
        if (numericUpDown1->Hexadecimal) {
            numericUpDown1->Hexadecimal = false;
        } else {
            numericUpDown1->Hexadecimal = true;
        }
    };
 

[JScript] 
function InstantiateMyNumericUpDown(){
  // Create and initialize a NumericUpDown control.
  numericUpDown1 = new NumericUpDown()
  
  // Dock the control to the top of the form.
  numericUpDown1.Dock = System.Windows.Forms.DockStyle.Top
  
  // Set the Minimum, Maximum, and initial Value.
  numericUpDown1.Value = 5
  numericUpDown1.Maximum = 2500
  numericUpDown1.Minimum = - 100
  
  // Add the NumericUpDown to the Form.
  Controls.Add(numericUpDown1)
}    

// Check box to toggle decimal places to be displayed.
function checkBox1_Click(sender : Object, e : EventArgs){
  // If DecimalPlaces is greater than 0, set them to 0 and round the
  // current Value; otherwise, set DecimalPlaces to 2 and change the
  // Increment to 0.25. 
  if(numericUpDown1.DecimalPlaces > 0){
    numericUpDown1.DecimalPlaces = 0
    numericUpDown1.Value = Decimal.Round(numericUpDown1.Value, 0)
  }else{
    numericUpDown1.DecimalPlaces = 2
    numericUpDown1.Increment = 0.25
  }
}    

// Check box to toggle thousands separators to be displayed.
function checkBox2_Click(sender : Object, e : EventArgs){
  // If ThousandsSeparator is true, set it to false;
  // otherwise, set it to true. 
  numericUpDown1.ThousandsSeparator = !numericUpDown1.ThousandsSeparator
}    

// Check box to toggle hexadecimal to be displayed.
function checkBox3_Click(sender : Object, e : EventArgs){
  // If Hexadecimal is true, set it to false;
  // otherwise, set it to true. 
  numericUpDown1.Hexadecimal = !numericUpDown1.Hexadecimal
}

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

See Also

NumericUpDown Members | System.Windows.Forms Namespace | UpDownBase | ISupportInitialize | DomainUpDown

Show:
© 2014 Microsoft