NumericUpDown

Programs sometimes need an arrow control. When this arrow is clicked, a number increments or decrement. The Windows Forms platform offers the NumericUpDown control. This features a textbox with a number and also two scroll buttons to change the value.

NumericUpDown

Get started

Note

To get started, please add a NumericUpDown control to your Windows Forms program in the Visual Studio designer by double-clicking on the entry in the Toolbox. Next, you can change some properties of the NumericUpDown by right-clicking on it and selecting Properties; these options are described in the latter sections. In this code example, we added the ValueChanged event handler by double-clicking on the NumericUpDown control.

Example of NumericUpDown and ValueChanged [C#]

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
	public Form1()
	{
	    InitializeComponent();
	}

	private void numericUpDown1_ValueChanged(object sender, EventArgs e)
	{
	    // You can change other program parts when the value changes.
	    this.Text = "Value changed to " + numericUpDown1.Value.ToString();
	}
    }
}

Get value of the NumericUpDown. In the numericUpDown1_ValueChanged method, you can see that the Value property is accessed on the numericUpDown1 instance. This returns a decimal that equals the value of the NumericUpDown instance. In your C# code, you will want to acquire the Value property of the NumericUpDown.

NumericUpDown and ValueChanged

Properties

Property (Icon copyright Microsoft)

The NumericUpDown control offers two properties called Minimum and Maximum. These are self-explanatory: they determine the lowest and highest value the NumericUpDown control can be scrolled to. By default, the NumericUpDown is limited to values of 0 to 100 inclusive.

Increment property. The Increment property determines how far the NumericUpDown will change its value up or down depending on the arrow clicked. Sometimes it may be useful to increase the number by more than 1 or less than one. You can use decimal places in the Increment property.

DecimalPlaces property. The DecimalPlaces property allows you to set the number of digits that come after the decimal place in the NumericUpDown control. Again, this property may be useful in some programs that use the NumericUpDown but not in many others.

ThousandsSeparator property. The ThousandsSeparator property gives you a culture-sensitive thousands separator: for an US English culture, this will cause the number 1000 to be displayed with a comma—1,000. This property would be useful for NumericUpDown controls that represent money amounts.

Hex

Hexadecimal property. It is possible to have the NumericUpDown control display its numbers in hexadecimal format using this property and setting it to True. With hexadecimal, the number 10 is displayed as the letter A; larger numbers are represented in fewer characters than by default.

InterceptArrowKeys property. This property gives you an option to disable having the arrow keys change the number value in the NumericUpDown control. Users will be able to use the arrow keys to change the location of the caret for editing the number when this is set to False. If you leave this on the default of True, arrow keys will change the number value up or down.

TextAlign and UpDownAlign. Finally, there are alignment properties available on the NumericUpDown control. The TextAlign property sets the position of the text in its part of the control; you can select Left, Right, or Center. The UpDownAlign property allows you to change the location of the arrow buttons; here you can select Left or Right.

Summary

.NET Framework information

As shown in this article, the NumericUpDown control provides a way to visually represent a numeric textbox that can be increased or decreased in value. With many different properties and event handlers, the NumericUpDown control is ideal for certain user interfaces where a number should be entered or adjusted.

Windows Forms
.NET