A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/c-sharp-numericupdown-class/ below:

C# - NumericUpDown Class - GeeksforGeeks

C# - NumericUpDown Class

Last Updated : 20 May, 2025

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:

Ways to Create a NumericUpDown In Windows Forms

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

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.

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.

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

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.

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

Output:

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:

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:

Properties 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 colour 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 colour 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 thousand 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.

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4