Open In App

C# - NumericUpDown Class

Last Updated : 20 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Windows Forms, the NumericUpDown control is used to provide a Windows spin box or an up-down control that displays the numeric values. The NumericUpDown control provides an interface that moves using an up and down arrow and holds a pre-defined numeric value. The NumericUpDown class in C# is used to represent the Windows numeric up-down box and also provides different types of properties, methods, and events. It is defined under System.Windows.Forms namespace.

Where is NumericUpDown Commonly Used?

NumericUpDown is commonly used in many apps in things like:

  • Picking time periods or limits for settings.
  • Changing options in desktop programs where numbers need to be entered within a set range.
  • Choosing age, amount, or percentage in forms.

Ways to Create a NumericUpDown In Windows Forms

There are mainly two ways to create a NumericUpDown in Windows Forms, which are mentioned below.

  • Drag and drop (Design-Time)
  • Custom NumericUpDown (Run-Time)

1. Drag and Drop (Design-Time)

This is the easiest way to create a NumericUpDown in Windows Forms using Visual Studio. We just have to open the toolbox and drag and drop the NumericUpDown control on the form in the designer, and further, we can change the appearance of the NumericUpDown using the properties. Follow these steps to create a NumericUpDown.

Step 1: Now, locate the project with the name. Here, we are using the default name, which is Form1, and it will open a form in the editor that we can further modify.

Empth-forms

In the image, we have two files that are open one Design and there is Form1.cs these two play a major role. We use the Form 1.cs file for the custom logic.


Step 2: Now open a Toolbox go to the view > Toolbox or ctrl + alt + x.

ToolBox



Step 3. Now open the common controls and drag and drop the NumericUpDown on the form where we want to it be placed.

DragAndDrop



Step 4. Now open the properties of the NumericUpDown contro press right-click on the NumericUpDown and it will open the properties solution explorer now we can change the appearance and the behaviour of the NumericUpDown.

PropertiesNumericUpDown

Now we can make different kinds of changes using properties and change the appearance.

Output:

exam1


2. Custom NumericUpDown (Run Time)

In this method, we are going to modify the Form1.cs file and add custom code modification in C# with the help of the GroupBox class. The following steps show how to create a GroupBox 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();


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


Step 3: Now add the NumericUpDown control to the form using the following statement:

// Adding this control

// to the form

this.Controls.Add(nbox);


Step 4: Now double-click on the form in Design and it will open the Form1.cs file where code is written in C#. Here, the program file is Form 1.cs. Now write this code in Form1.cs file.

Form1.cs file:

C#
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += Form1_Load; // Subscribe to form load event
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Create and configure a label to describe the form
            Label formLabel = new Label();
            formLabel.Location = new Point(348, 61);
            formLabel.Size = new Size(215, 20);
            formLabel.Text = "Form";
            formLabel.Font = new Font("Bodoni MT", 12);
            this.Controls.Add(formLabel);

            // Create and configure a label for the NumericUpDown
            Label ageLabel = new Label();
            ageLabel.Location = new Point(242, 136);
            ageLabel.Size = new Size(103, 20);
            ageLabel.Text = "Enter Age";
            ageLabel.Font = new Font("Bodoni MT", 12);
            this.Controls.Add(ageLabel);

            // Create the NumericUpDown control
            NumericUpDown numericUpDown = new NumericUpDown();

            // Set location and size on the form
            numericUpDown.Location = new Point(386, 130);
            numericUpDown.Size = new Size(126, 26);

            // Set font style for better readability
            numericUpDown.Font = new Font("Bodoni MT", 12);

            // Set initial value, minimum and maximum limits
            numericUpDown.Value = 18;
            numericUpDown.Minimum = 18;
            numericUpDown.Maximum = 30;

            // Customize colors for better UI experience
            numericUpDown.BackColor = Color.LightGreen;
            numericUpDown.ForeColor = Color.DarkGreen;

            // Set increment for each click on up/down buttons
            numericUpDown.Increment = 1;

            // Assign a meaningful name for reference
            numericUpDown.Name = "MySpinBox";

            // Add the NumericUpDown control to the form
            this.Controls.Add(numericUpDown);
        }
    }
}


Output:

Output


Constructor

This class consists one constructor, with the help of which we can create objects of this class. The constructor available in this class is listed below:

NumericUpDown(): This constructor is used to initialize a new instance of the NumericUpDown class.

Syntax:

NumericUpDown()


Properties

The properties of this class is listed below:

PropertiesDescription
AutoSizeThis property is used to get or set a value that indicates whether the control resizes based on its contents.
BackColorThis property is used to get or set the background colour for the control.
BorderStyleThis property indicates the border style for the control.
FontThis property is used to get or set the font of the text displayed by the control.
ForeColorThis property is used to get or set the foreground colour of the control.
HeightThis property is used to get or set the height of the control.
LocationThis 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.
NameThis property is used to get or set the name of the control.
TabStopThis 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.
SizeThis property is used to get or set the height and width of the control.
TextThis property is used to get or set the text to be displayed in the NumericUpDown control.
TextAlignThis property is used to get or set the alignment of the text in the spin box (also known as an up-down control).
VisibleThis property is used to get or set a value indicating whether the control and all its child controls are displayed.
WidthThis property is used to get or set the width of the control.
UpDownAlignThis 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).
ThousandsSeparatorThis property is used to get or set a value indicating whether a thousand separator is displayed in the spin box (also known as an up-down control) when appropriate.
HexadecimalThis 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.
IncrementThis 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.

Similar Reads