Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Design-Time:
3.
Run-Time
4.
Constructor
5.
Properties
6.
Frequently Asked Questions
6.1.
What is the use of NumericUpDown Class in C#?
6.2.
What is the advantage of using NumericUpDown Class?
6.3.
What is NumericUpDown Class in C#?
6.4.
How can we add control to NumericUpDown?
6.5.
What is numeric updown in VB net?
7.
Conclusion
Last Updated: Mar 27, 2024

C# NumericUpDown Class

Author Aditya Singh
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

The computer language C# (pronounced "See Sharp") is modern, object-oriented, and type-safe. C# allows programmers to create a wide range of secure and reliable programmes for use in.

The NumericUpDown control is used in Windows Forms to give a Windows spin box or an up-down control that shows numeric values. In other words, the NumericUpDown control creates an interface that travels up and down using arrows and holds a numeric value. This class is used to represent the numeric up-down box in Windows, as well as providing many attributes, methods, and events. The namespace System.Windows.Forms contain their definition.

In C#, there are two methods for creating a NumericUpDown in a Windows form:

1- Design-Time

2- Run-Time

Recommended topics, Palindrome in C# and Ienumerable vs Iqueryable

Design-Time:

Simply drag and drop a NumericUpDown control from the Toolbox to a Form in Visual Studio to build a NumericUpDown control at design time. When you drag and drop a NumericUpDown on a Form, the NumericUpDown appears, as seen in the figure below. You can move and resize a NumericUpDown once it's on the Form, as well as set its settings and events.

Follow the following steps to create a NumericUpDown as shown in the image below:

Step 1: As demonstrated in the figure below, create a windows form:

Visual Studio --> File --> New --> Project --> WindowsFormApp-->Give Project Name --> Click Create

 

 

 

Step 2: Then, from the Toolbox, drag and drop the NumericUpDown control onto the form.

 

 

Step 3: You will go to the properties of the NumericUpDown control after dragging and dropping to edit NumericUpDown according to your requirements.

 

 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Run-Time

It's a little more complex than the previous way. With the help of the NumericUpDown class's syntax, you may construct a NumericUpDown control programmatically in this method.

The instructions below demonstrate how to set the generate NumericUpDown dynamically:

Step 1: Create a NumericUpDown control using the NumericUpDown() constructor which is given by the NumericUpDown Class to us.

// For creating a NumericUpDown control in C#

NumericUpDown wform = new NumericUpDown();

Step 2: Set the property of the NumericUpDown control provided by the NumericUpDown class after establishing a NumericUpDown control.
 

// Setting up the properties of NumericUpDown control which is wform here 
wform.Location = new Point(386, 130); 
wform.Size = new Size(126, 26); 
wform.Font = new Font("Bodoni MT", 12); 
wform.Value = 20; 
wform.Minimum = 20; 
wform.Maximum = 40; 
wform.BackColor = Color.LightGreen; 
wform.ForeColor = Color.DarkGreen; 
wform.Increment = 2; 
wform.Name = "Myform";

 

Step 3: Finally, use the following statement to add this NumericUpDown control to the form:

// Adding this control 
// to the form 
this.Controls.Add(wform);

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 up the properties of the labels
      Label li = new Label();
      li.Location = new Point(348, 61);
      li.Size = new Size(215, 20);
      li.Text = "Form For Number";
      li.Font = new Font("Bodoni MT", 12);
      this.Controls.Add(li);

      Label lj = new Label();
      lj.Location = new Point(250, 140);
      lj.Size = new Size(110, 200);
      lj.Text = "Enter Your Lucky Number";
      lj.Font = new Font("Bodoni MT", 16);
      this.Controls.Add(lj);

      // Creating and setting up the properties of NumericUpDown

      NumericUpDown wform = new NumericUpDown();
      wform.Location = new Point(386, 130);
      wform.Size = new Size(126, 26);
      wform.Font = new Font("Bodoni MT", 12);
      wform.Value = 18;
      wform.Minimum = 18;
      wform.Maximum = 30;
      wform.BackColor = Color.LightGreen;
      wform.ForeColor = Color.DarkGreen;
      wform.Increment = 1;
      wform.Name = "MySpinBox";

      // Adding this control
      // to the form
      this.Controls.Add(wform);
    }
  }
}

 

Constructor

Constructor

Description

NumericUpDown()

This Constructor is used to create a new NumericUpDown class instance.

Properties

Property

Description

AutoSize

This property gets a value that specifies whether the control resizes in response to its contents.

BackColor

This attribute is used to acquire or set the control's background color.

BorderStyle

This attribute specifies the control's border style

Font

The font of the text displayed by the control can be obtained or changed using this attribute.

ForeColor

This property is used to get or set the foreground color of the control.

Height

The height of the control is obtained or set using this property.

Location

TThis attribute is used to acquire or set the NumericUpDown control's upper-left corner concerning the form's upper-left corner.

Name

The name of the control is retrieved or set using this attribute.

TabStop

This property is used to acquire or update a value that indicates if the user can concentrate the NumericUpDown by pressing the TAB key.

Size

This property is used to get or set the control's height and width.

Text

The text to be shown in the NumericUpDown control can be retrieved or modified using this attribute.

TextAlign

TThis property is used to get or set the text in the spin box's alignment (an up-down control).

Visible

This property gets or sets a value that indicates whether the control and all of its child controls are visible.

Frequently Asked Questions

What is the use of NumericUpDown Class in C#?

Users can give a spin (up/down) interface to traverse between predefined numbers using up and down arrows using a NumericUpDown control.

What is the advantage of using NumericUpDown Class?

The NumericUpDown control in Windows Forms resembles a text box with a pair of arrows that the user can use to alter a value. From a list of options, the control displays and sets a single numeric value. By clicking up and down buttons, hitting the UP and DOWN ARROW keys, or inputting a number, the user can increase or decrease the number. The UP ARROW key raises the value to its highest point, while the DOWN ARROW key lowers it to its lowest point. Volume control on a music player is an example of where this type of control might be useful. Some Windows control panel programs employ numerical up-down controls.

What is NumericUpDown Class in C#?

A NumericUpDown control has a single numeric value that may be increased or decreased by using the controls up and down buttons. Unless the ReadOnly attribute is set to true, the user can also enter a value

How can we add control to NumericUpDown?

Simply drag and drop a NumericUpDown control from the Toolbox to a Form in Visual Studio to build a NumericUpDown control at design time. Or we can do with the coding parameters in the run-time method.

What is numeric updown in VB net?

NumericUpDown is a counter that allows the user to enter a number within a certain range. NumericUpDown Control allows us to enter numeric values and includes additional capabilities such as up-down buttons and accelerated auto-repeat.

Conclusion

In this article, we discussed NumericUpDown Class in C#. We also discussed different ways to create a NumericUpDown. And finally, in the end, we have shown an example of how to create a data showing form in NumericUpDown.

We hope that our blog has helped you enhance your knowledge regarding NumericUpDown Class in C#. Please check out these articles to improve your understanding of C#: Static Class in C#Operators in C#Properties of C#, singleton design pattern in c#  

 

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Please upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass