📜  C#| NumericUpDown类别

📅  最后修改于: 2021-05-29 18:28:35             🧑  作者: Mango

在Windows窗体中,NumericUpDown控件用于提供Windows旋转框或显示数字值的上下控件。换句话说,NumericUpDown控件提供了一个界面,该界面使用向上和向下箭头移动并保存一些预定义的数值。 NumericUpDown类用于表示Windows数字上下框,还提供不同类型的属性,方法和事件。它在System.Windows.Forms命名空间下定义。在C#中,您可以使用两种不同的方法在Windows窗体中创建NumericUpDown:

1.设计时:这是创建NumericUpDown的最简单方法,如以下步骤所示:

  • 第1步:创建一个Windows窗体,如下图所示:
    Visual Studio->文件->新建->项目-> WindowsFormApp
  • 步骤2:接下来,将NumericUpDown控件从工具箱拖放到窗体。

  • 步骤3:拖放之后,您将转到NumericUpDown控件的属性,以根据需要修改NumericUpDown。

    输出:

2.运行时:比上述方法有些棘手。在此方法中,可以借助NumericUpDown类提供的语法以编程方式创建NumericUpDown控件。以下步骤显示如何动态设置创建NumericUpDown:

  • 步骤1:使用NumericUpDown类提供的NumericUpDown()构造函数创建NumericUpDown控件。
    // Creating a NumericUpDown control
    NumericUpDown nbox = new NumericUpDown(); 
    
  • 步骤2:创建NumericUpDown控件后,设置NumericUpDown类提供的NumericUpDown控件的属性。
    // Setting the properties of NumericUpDown control
    nbox.Location = new Point(386, 130); 
    nbox.Size = new Size(126, 26); 
    nbox.Font = new Font("Bodoni MT", 12); 
    nbox.Value = 18; 
    nbox.Minimum = 18; 
    nbox.Maximum = 30; 
    nbox.BackColor = Color.LightGreen; 
    nbox.ForeColor = Color.DarkGreen; 
    nbox.Increment = 1; 
    nbox.Name = "MySpinBox"; 
    
  • 步骤3:最后,使用以下语句将此NumericUpDown控件添加到表单中:
    // Adding this control 
    // to the form 
    this.Controls.Add(nbox); 
    

    例子:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
      
    namespace WindowsFormsApp42 {
      
    public partial class Form1 : Form {
      
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
            // Creating and setting the
            // properties of the labels
            Label l1 = new Label();
            l1.Location = new Point(348, 61);
            l1.Size = new Size(215, 20);
            l1.Text = "Form";
            l1.Font = new Font("Bodoni MT", 12);
            this.Controls.Add(l1);
      
            Label l2 = new Label();
            l2.Location = new Point(242, 136);
            l2.Size = new Size(103, 20);
            l2.Text = "Enter Age";
            l2.Font = new Font("Bodoni MT", 12);
            this.Controls.Add(l2);
      
            // Creating and setting the
            // properties of NumericUpDown
            NumericUpDown nbox = new NumericUpDown();
            nbox.Location = new Point(386, 130);
            nbox.Size = new Size(126, 26);
            nbox.Font = new Font("Bodoni MT", 12);
            nbox.Value = 18;
            nbox.Minimum = 18;
            nbox.Maximum = 30;
            nbox.BackColor = Color.LightGreen;
            nbox.ForeColor = Color.DarkGreen;
            nbox.Increment = 1;
            nbox.Name = "MySpinBox";
      
            // Adding this control
            // to the form
            this.Controls.Add(nbox);
        }
    }
    }
    

    输出:

建设者

Constructor Description
NumericUpDown() This Constructors is used to initialize a new instance of the NumericUpDown class.

特性

Property Description
AutoSize This property is used to get or set a value that indicates whether the control resizes based on its contents.
BackColor This property is used to get or set the background color for the control.
BorderStyle This property indicates the border style for the control.
Font This property is used to get or set the font of the text displayed by the control.
ForeColor This property is used to get or set the foreground color of the control.
Height This property is used to get or set the height of the control.
Location This property is used to get or set the coordinates of the upper-left corner of the NumericUpDown control relative to the upper-left corner of its form.
Name This property is used to get or set the name of the control.
TabStop This property is used to get or set a value that shows whether the user can press the TAB key to provide the focus to the NumericUpDown.
Size This property is used to get or set the height and width of the control.
Text This property is used to get or set the text to be displayed in the NumericUpDown control.
TextAlign This property is used to get or set the alignment of the text in the spin box (also known as an up-down control).
Visible This property is used to get or set a value indicating whether the control and all its child controls are displayed.
Width This property is used to get or set the width of the control.
UpDownAlign This property is used to get or set the alignment of the up and down buttons on the spin box (also known as an up-down control).
ThousandsSeparator This property is used to get or set a value indicating whether a thousands separator is displayed in the spin box (also known as an up-down control) when appropriate.
Hexadecimal This property is used to get or set a value indicating whether the spin box (also known as an up-down control) should display the value it contains in hexadecimal format.
Increment This property is used to get or set the value to increment or decrement the spin box (also known as an up-down control) when the up or down buttons are clicked.