Open In App

How to set Minimum Value in NumericUpDown in C#?

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. In NumericUpDown control, you can set the minimum value for the up-down control using Maximum Property. The default value of this property is 0. You can set this property in two different ways:
1. Design-Time: It is the easiest way to set the minimum value for the NumericUpDown as shown in the following steps:





2. Run-Time: It is a little bit trickier than the above method. In this method, you can set the minimum value for the NumericUpDown control programmatically with the help of given syntax: 

public decimal Minimum { get; set; }

Here, the value of this property represents the minimum value of the NumericUpDown. The following steps show how to set the minimum value for the NumericUpDown dynamically:

// Creating a NumericUpDown
NumericUpDown n = new NumericUpDown();
// Setting the minimum value
n.Minimum = 18;
// Adding NumericUpDown control on the form
this.Controls.Add(n);




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 n = new NumericUpDown();
        n.Location = new Point(386, 130);
        n.Size = new Size(126, 26);
        n.Font = new Font("Bodoni MT", 12);
        n.Value = 18;
        n.Minimum = 18;
        n.Maximum = 30;
        n.BackColor = Color.LightGreen;
        n.ForeColor = Color.DarkGreen;
        n.Increment = 1;
        n.Name = "MySpinBox";
 
        // Adding this control
        // to the form
        this.Controls.Add(n);
    }
}
}


Article Tags :
C#