一个NumericUpDown控件包含可以通过单击向上或向下按钮的控件是递增或递减的单个数字值。 用户还可以输入一个值,除非ReadOnly属性设置为true
。
可以通过设置格式显示数字DecimalPlaces, Hexadecimal,或ThousandsSeparator属性。 若要在控件中显示的十六进制值,设置Hexadecimal属性设置为true
。 若要显示千位分隔符在适当时的十进制数字中设置ThousandsSeparator属性设置为true
。 若要指定十进制符号的后面显示的数字,将设置DecimalPlaces属性设置为要显示的小数位数数字。
若要指定允许的范围的控件的值,设置Minimum和Maximum属性。 设置Increment值,以指定的值是递增还是递减为Value属性当用户单击向上或向下箭头按钮。 您可以提高控件移动通过数字时用户连续按向上或向下箭头,通过设置速度Accelerations属性。
当UpButton或DownButton调用方法,在代码中或通过单击向上或向下按钮,新值进行验证,并使用适当的格式中的新值更新的控件。 具体而言,如果UserEdit属性设置为true
,则ParseEditText之前验证或更新的值调用方法。 然后验证值必须介于Minimum并Maximum值,和UpdateEditText调用方法。
以上文字摘录自官方文档,下面通过一个简单的例子来看看这个控件如何使用
1.界面布局
界面布局如下
一个NumericUpDown和一个TextBox控件,TextBox控件用来同步显示内容
2.用法示例
代码也比较简单,这里我们设置小数点后显示2位,每次增加0.25,代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
numericUpDown1.DecimalPlaces = 2;
numericUpDown1.Increment = 0.25M;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
textBox1.Text = numericUpDown1.Value.ToString();
}
}
}