C# Windows Forms NumericUpDown Examples

Let us look at a simple C# Windows Forms NumericUpDown example.

Example 1: NumericUpDown

This example will comprise the following files:

  • NumericUpDown.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). NumericUpDown.cs

Create a file named NumericUpDown.cs

Here is the full code

using System;
using System.Windows.Forms;

namespace Examples {
  class MainForm : Form {
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new MainForm());
    }

    public MainForm() {
      Text = "NumericUpDown example";

      numericUpDown1.Parent = this;
      numericUpDown1.DecimalPlaces = 0;
      numericUpDown1.Location = new System.Drawing.Point(80, 50);
      numericUpDown1.Value = 50;
      numericUpDown1.ValueChanged += delegate {
        System.Diagnostics.Debug.WriteLine(string.Format("value = {0}", numericUpDown1.Value));
      };

      numericUpDown1.TextChanged += delegate {
        System.Diagnostics.Debug.WriteLine(string.Format("text = {0}", numericUpDown1.Text));
      };

      numericUpDown2.Parent = this;
      numericUpDown2.DecimalPlaces = 2;
      numericUpDown2.Increment = 0.01M;
      numericUpDown2.Location = new System.Drawing.Point(80, 100);
      numericUpDown2.Maximum = 11.0M;
      numericUpDown2.Minimum = 10.0M;
    }

    private NumericUpDown numericUpDown1 = new NumericUpDown();
    private NumericUpDown numericUpDown2 = new NumericUpDown();
  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Related Posts