C# | NumericUpDown Class - GeeksforGeeks

05 Sep, 2019
In Windows Forms, NumericUpDown control is used to provide a Windows spin box or an up-down control which displays the numeric values. Or in other words, NumericUpDown control provides an interface which moves using up and down arrow and holds some pre-defined numeric value. The NumericUpDown class is used to represent the windows numeric up-down box and also provide different types of properties, methods, and events. It is defined under System.Windows.Forms namespace. In C# you can create a NumericUpDown in the windows form by using two different ways:
1. Design-Time: It is the easiest way to create a NumericUpDown as shown in the following steps:
Step 1: Create a windows form as shown in the below image:Visual Studio -> File -> New -> Project -> WindowsFormApp
Step 2: Next, drag and drop the NumericUpDown control from the toolbox to the form.
Step 3: After drag and drop you will go to the properties of the NumericUpDown control to modify NumericUpDown according to your requirement.Output:
Output:
2. Run-Time: It is a little bit trickier than the above method. In this method, you can create a NumericUpDown control programmatically with the help of syntax provided by the NumericUpDown class. The following steps show how to set the create NumericUpDown dynamically:
Step 1: Create a NumericUpDown control using the NumericUpDown() constructor is provided by the NumericUpDown class.// Creating a NumericUpDown control
NumericUpDown nbox = new NumericUpDown(); 

// Creating a NumericUpDown control
NumericUpDown nbox = new NumericUpDown(); 

Step 2: After creating a NumericUpDown control, set the property of the NumericUpDown control provided by the NumericUpDown class.// 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"; 

// 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"; 

Step 3: And last add this NumericUpDown control to the form using the following statement:// Adding this control 
// to the form 
this.Controls.Add(nbox); 
Example: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);    }}}Output:
// Adding this control 
// to the form 
this.Controls.Add(nbox); 

Example:
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);    }}}
Output:
CSharp-Windows-Forms-Namespace
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Destructors in C#
Difference between Ref and Out keywords in C#
C# | Delegates
C# | Constructors
Extension Method in C#
C# | String.IndexOf( ) Method | Set - 1
C# | Class and Object
Introduction to .NET Framework
C# | Abstract Classes
C# | Data Types