C# NumericUpDown Control: Windows Forms

Use the NumericUpDown control to add a narrow control to adjust a number.

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—a textbox with a number and two scroll buttons to change the value.

Start. Please add a NumericUpDown control to your program in the Visual Studio designer by double-clicking on the entry in the Toolbox. Next, you can change properties of the NumericUpDown by right-clicking on it and selecting Properties.

Here: 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(); } } }

In numericUpDown1_ValueChanged, the Value property is accessed on the numericUpDown1 instance. This returns a decimal that equals the value of the NumericUpDown instance. In C# code, we acquire the Value property of the NumericUpDown.

Properties. NumericUpDown offers two properties called Minimum and Maximum. 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.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.

Note: It is possible to use numbers that have decimal places in the Increment 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.

The ThousandsSeparator property gives you a culture-sensitive thousands separator. For a US English culture, this will cause the number 1000 to be displayed with a comma. We see the value 1,000.

Tip: This property would be useful for NumericUpDown controls that represent money amounts.

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.Format: Hex

InterceptArrowKeys. This allows you to disable having the arrow keys change the number value in the NumericUpDown control. When true, users can press arrow keys to change the location of the caret for editing the number.

And: If you leave this on the default of True, arrow keys will change the number value up or down.

Align. 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.

And: The UpDownAlign property allows you to change the location of the arrow buttons—here you can select Left or Right.

Summary. NumericUpDown represents a numeric textbox that can be increased or decreased in value. With many properties and event handlers, the NumericUpDown control is ideal for user interfaces where a number is entered or adjusted.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to