Home > Articles > Programming > User Interface (UI)

This chapter is from the book

Using the NumericUpDown Control

The NumericUpDown control is a simple way to give the user a way to select a number that falls between a minimum and a maximum value. The control can accept only integers, and decimal values will be truncated as opposed to rounded. On the Pocket PC, the maximum value cannot be greater than that of a 16-bit signed integer.

The NumericUpDown control is controlled by four integer properties: Minimum, Maximum, Value, and Increment. The Minimum and Maximum properties define the minimum and maximum values of the control. The Value property is the current value of the control. The Increment property defines the amount by which the current value is incremented or decremented when the user clicks the up or down arrow buttons. The current value is always incremented or decremented by the Increment value, unless the resulting value would be out of the range defined by the Minimum and Maximum values.

The user can also change the Value property by typing a new value into the control. If the value that the user types is between the Minimum and Maximum values, then both the Value and Text properties will be changed to reflect the newly entered value. If the new value is outside the set range, then the Text property takes the entered value, whereas the Value property becomes equal to the Maximum property. To stop users from typing data into the control altogether, set the ReadOnly property to true.

When a user changes the value of the NumericUpDown control, a ValueChanged event is fired. The ValueChanged event is fired only when the value is changed through code or via the up and down arrows. The event will not be fired when a user types input into the control. Listing 3.2 demonstrates how to use the NumericUpDown control and how to handle the ValueChanged event:

Listing 3.2

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace NumericUpDown
 public class Form1 : System.Windows.Forms.Form
  private System.Windows.Forms.NumericUpDown numericUpDown1;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.MainMenu mainMenu1;

  public Form1()

  protected override void Dispose( bool disposing )
   base.Dispose( disposing );
  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
   this.mainMenu1 = new System.Windows.Forms.MainMenu();
   this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   // numericUpDown1
   this.numericUpDown1.Location = new System.Drawing.Point(8, 56);
   this.numericUpDown1.Maximum =
       new System.Decimal(new int[] {2003, 0, 0, 0});
   this.numericUpDown1.Minimum =
       new System.Decimal(new int[] {1900, 0, 0, 0});
   this.numericUpDown1.Value =
       new System.Decimal(new int[] {190012, 0, 0, 131072});
   this.numericUpDown1.ValueChanged += 
    new System.EventHandler(this.numericUpDown1_ValueChanged);
   // label1
   this.label1.Location = new System.Drawing.Point(8, 24);
   this.label1.Size = new System.Drawing.Size(184, 16);
   this.label1.Text = "In what year were you born?";
   this. label1.ParentChanged +=
       new System.EventHandler(this.label1_ParentChanged);
   // label2
   this.label2.Location = new System.Drawing.Point(8, 120);
   this.label2.Size = new System.Drawing.Size(224, 24);
   // Form1
   this.Menu = this.mainMenu1;
   this.Text = "Form1";


  static void Main()
   Application.Run(new Form1());

  static string msg = "You are ~{0} years young.";
  private void numericUpDown1_ValueChanged(object sender, System.EventArgs e)
   int yearsOld = System.DateTime.Now.Year - (int)this.numericUpDown1.Value;
   this.label2.Text = String.Format(msg, yearsOld);
Public Class Form1
 Inherits System.Windows.Forms.Form
 Friend WithEvents label2 As System.Windows.Forms.Label
 Friend WithEvents label1 As System.Windows.Forms.Label
 Friend WithEvents numericUpDown1 As System.Windows.Forms.NumericUpDown
 Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu

#Region " Windows Form Designer generated code "

 Public Sub New()

  'This call is required by the Windows Form Designer.

  'Add any initialization after the InitializeComponent() call

 End Sub

 'Form overrides dispose to clean up the component list.
 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
 End Sub

 'NOTE: The following procedure is required by the Windows Form Designer
 'It can be modified using the Windows Form Designer.
 'Do not modify it using the code editor.
 Private Sub InitializeComponent()
  Me.MainMenu1 = New System.Windows.Forms.MainMenu
  Me.label2 = New System.Windows.Forms.Label
  Me.label1 = New System.Windows.Forms.Label
  Me.numericUpDown1 = New System.Windows.Forms.NumericUpDown
   Me.label2.Location = New System.Drawing.Point(8, 104)
   Me.label2.Size = New System.Drawing.Size(224, 24)
   Me.label1.Location = New System.Drawing.Point(8, 8)
   Me.label1.Size = New System.Drawing.Size(184, 16)
   Me.label1.Text = "In what year were you born?"
   Me.numericUpDown1.Location = New System.Drawing.Point(8, 40)
   Me.numericUpDown1.Maximum = New Decimal(New Integer() {2003, 0, 0, 0})
   Me.numericUpDown1.Minimum = New Decimal(New Integer() {1900, 0, 0, 0})
   Me.numericUpDown1.Value =
       New Decimal(New Integer() {190012, 0, 0, 131072})
   Me.Menu = Me.MainMenu1
   Me.Text = "Form1"
 End Sub

#End Region

 Private Sub _
 (ByVal sender As System.Object, ByVal e As System.EventArgs)
 Handles numericUpDown1.ValueChanged
  Dim yearsOld As Int32
  yearsOld = System.DateTime.Now.Year - numericUpDown1.Value
  label2.Text = String.Format("You are ~{0} years young.",
 End Sub
End Class

Figure 3.10 displays the application running on the Pocket PC 2002 emulator.

Figure 3.10Figure 3.10 An application that showcases the NumericUpDown control running on the Pocket PC 2002 emulator.

  • + Share This
  • 🔖 Save To Your Account