NumericUpDown 类

表示显示数值的 Windows 数字显示框(也称作 up-down 控件)。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class NumericUpDown
    Inherits UpDownBase
    Implements ISupportInitialize
用法
Dim instance As NumericUpDown
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class NumericUpDown : UpDownBase, ISupportInitialize
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
public ref class NumericUpDown : public UpDownBase, ISupportInitialize
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class NumericUpDown extends UpDownBase implements ISupportInitialize
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class NumericUpDown extends UpDownBase implements ISupportInitialize

备注

一个包含单个数值的 NumericUpDown 控件,通过单击该控件的向上或向下按钮可使该数值递增或递减。如果未将 ReadOnly 属性设置为 true,则用户也可输入一个值。

可通过设置 DecimalPlacesHexadecimalThousandsSeparator 属性来设置数字的显示格式。若要在控件中显示十六进制值,请将 Hexadecimal 属性设置为 true。若要在适当的时候显示十进制数的千分隔符,请将 ThousandsSeparator 属性设置为 true。若要指定小数点后显示的位数,请将 DecimalPlaces 属性设置为要显示的小数位数。

若要指定控件允许值的范围,请设置 MinimumMaximum 属性。设置 Increment 值,以指定在用户单击向上或向下箭头按钮时将向 Value 属性递增或从其递减的值。您可以通过设置 Accelerations 属性,提高用户连续按向上或向下箭头时该控件在数字间移动的速度。

当调用 UpButtonDownButton 方法时,不论是用代码还是通过单击向上或向下箭头键,都会对新值进行验证,而且会以适当的格式用新值对控件进行更新。具体而言,如果 UserEdit 属性设置为 true,则在验证或更新该值之前,将调用 ParseEditText 方法。然后,验证该值是否在 MinimumMaximum 两个值之间,并调用 UpdateEditText 方法。

示例

下面的代码示例创建并初始化一个 NumericUpDown 控件,设置它的一些公共属性,并使用户可以在运行时更改其中的某些属性。此代码假定已经在窗体上放置三个 CheckBox 控件,并且已经实例化它们的 Click 事件的处理程序。DecimalPlacesThousandsSeparatorHexadecimal 属性在每个复选框的 Click 事件上设置。

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
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;
   }
}
public:
   void InstantiateMyNumericUpDown()
   {
      // Create and initialize a NumericUpDown control.
      numericUpDown1 = gcnew 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 );
   }

private:
   // Check box to toggle decimal places to be displayed.
   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 = Decimal(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;
      }
   }
public void InstantiateMyNumericUpDown()
{
    // Create and initialize a NumericUpDown control.
    numericUpDown1 = new NumericUpDown();

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

    // Set the Minimum, Maximum, and initial Value.
    numericUpDown1.set_Value(System.Convert.ToDecimal(5));
    numericUpDown1.set_Maximum(System.Convert.ToDecimal(2500));
    numericUpDown1.set_Minimum(System.Convert.ToDecimal(-100));

    // Add the NumericUpDown to the Form.
    get_Controls().Add(numericUpDown1);
} //InstantiateMyNumericUpDown

// 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.get_DecimalPlaces() > 0) {
        numericUpDown1.set_DecimalPlaces(0);
        numericUpDown1.set_Value(Decimal.Round
            (numericUpDown1.get_Value(), 0));
    }
    else {
        numericUpDown1.set_DecimalPlaces(2);
        numericUpDown1.set_Increment(System.Convert.ToDecimal((0.25)));
    }
} //checkBox1_Click

// 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.get_ThousandsSeparator()) {
        numericUpDown1.set_ThousandsSeparator(false);
    }
    else {
        numericUpDown1.set_ThousandsSeparator(true);
    }
} //checkBox2_Click

// 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.get_Hexadecimal()) {
        numericUpDown1.set_Hexadecimal(false);
    }
    else {
        numericUpDown1.set_Hexadecimal(true);
    }
} //checkBox3_Click
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
}

继承层次结构

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 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

NumericUpDown 成员
System.Windows.Forms 命名空间
UpDownBase
ISupportInitialize
DomainUpDown 类