C#中NumericUpDown 控件应用实例
NumericUpDown 控件
1.功能
NumericUpDown控件看起来像是一个文本框与一对箭头的组合,用户可以单击箭头来调整数值。NumericUpDown控件显示并设置选择列表中的单个数值,用户可以通过单击向上和向下按钮、按向上键和向下键或键入一个数字来增大和减小数字。单击向上键时,值沿最大值方向增加;单击向下键时,值沿最小值方向减少。图1所示为NumericUpDown控件。

图1 NumericUpDown控件
2.属性
NumericUpDown控件常用属性及说明如表1所示。

表1 NumericUpDown控件常用属性及说明
下面对比较重要的属性进行详细介绍。
(1)Maximum属性。
语法:
public decimal Maximum { get; set; }
属性值:数字显示框的最大值。默认值为100。
(2)Minimum属性。
语法:
public decimal Minimum { get; set; }
属性值:数字显示框的最小允许值。默认值为0。
(3)Value属性。
语法:
public decimal Value { get; set; }
属性值:NumericUpDown控件的数值。
(4)Increment属性。
语法:
public decimal Increment { get; set; }
属性值:单击数字显示框上的向上或向下按钮时,Value属性要递增或递减的值。默认值为1。
示例
NumericUpDown控件属性的使用
本示例主要介绍如何在Windows窗体中设置NumericUpDown控件的属性,运行结果如图2所示。

图2 NumericUpDown控件属性的使用
程序主要代码如下:
private void frmNumericUpDow_Load(object sender, EventArgs e)
{
this.numericUpDown1.Maximum = 20;
this.numericUpDown1.Minimum = 1;
this.numericUpDown1.Increment = 2;
this.numericUpDown1.Value = 4;
}
完整程序代码如下:
★★★★★主程序文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace _8_09
{
static class Program
{
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmNumericUpDow());
}
}
}
★★★★★frmNumericUpDow窗体设计文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace _8_09
{
public partial class frmNumericUpDow : Form
{
public frmNumericUpDow()
{
InitializeComponent();
}
private void frmNumericUpDow_Load(object sender, EventArgs e)
{
this.numericUpDown1.Maximum = 20;
this.numericUpDown1.Minimum = 1;
this.numericUpDown1.Increment = 2;
this.numericUpDown1.Value = 4;
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
}
}
}
★★★★★frmNumericUpDow窗体代码文件完整程序代码★★★★★
namespace _8_09
{
partial class frmNumericUpDow
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。