14.10 NumericUpDown Control

At times, you’ll want to restrict a user’s input choices to a specific range of numeric values. This is the purpose of the NumericUpDown control. This control appears as a TextBox, with two small Buttons on the right side—one with an up arrow and one with a down arrow. By default, a user can type numeric values into this control as if it were a TextBox or click the up and down arrows to increase or decrease the value in the control, respectively. The largest and smallest values in the range are specified with the Maximum and Minimum properties, respectively (both of type decimal). The Increment property (also of type decimal) specifies by how much the current value changes when the user clicks the arrows. Property DecimalPlaces specifies the number of decimal places that the control should display. Figure 14.35 describes common NumericUpDown properties and an event.

Fig. 14.35 NumericUpDown properties and an event.

NumericUpDown properties and an event Description
Common Properties
DecimalPlaces Specifies how many decimal places to display in the control.
Increment Specifies by how much the current number in the control changes when the user clicks the control’s up and down arrows.
Maximum Largest value in the control’s range.
Minimum Smallest value in the control’s range.
UpDownAlign Modifies the alignment of the up and down Buttons on the NumericUpDown control. This property can be used to display these Buttons either to the left or to the right of the control.
Value The numeric value currently displayed in the control.
Common Event
ValueChanged This event is raised when the value in the control is changed. This is the default event for the NumericUpDown control.

Figure 14.36 demonstrates a NumericUpDown control in a GUI that calculates interest rate. The calculations performed in this app are similar to those in Fig. 6.6. TextBoxes are used to input the principal and interest rate amounts, and a NumericUpDown control is used to input the number of years for which we want to calculate interest.

Fig. 14.36 Demonstrating the NumericUpDown control.

Alternate View

  1    // Fig. 14.36: InterestCalculatorForm.cs
  2    // Demonstrating the NumericUpDown control.
  3    using System;
  4    using System.Windows.Forms;
  5
  6    namespace NumericUpDownTest
  7    {
  8        public partial class InterestCalculatorForm : Form
  9        {
 10            // default constructor
 11            public InterestCalculatorForm()
 12            {
 13                InitializeComponent();
 14            }
 15
 16            private void calculateButton_Click(object sender, EventArgs e)
 17            {
 18                // retrieve user input
 19                decimal principal = decimal.Parse(principalTextBox.Text);
 20                double rate = double.Parse(interestTextBox.Text);
 21                int year = (int) yearUpDown.Value;
 22
 23                // set output header
 24                string output = "Year	Amount on Deposit
";
 25
 26                // calculate amount after each year and append to output
 27                for (int yearCounter = 1; yearCounter <= year; ++yearCounter)
 28                {
 29                    decimal amount = principal *
 30                    ((decimal) Math.Pow((1 + rate / 100), yearCounter));
 31                    output += $"{yearCounter}t{amount:C}
";
 32                }
 33
 34                displayTextBox.Text = output; // display result
 35            }
 36        }
 37    }

For the NumericUpDown control named yearUpDown, we set the Minimum property to 1 and the Maximum property to 10. We left the Increment property set to 1, its default value. These settings specify that users can enter a number of years in the range 1 to 10 in increments of 1. If we had set the Increment to 0.5, we could also input values such as 1.5 or 2.5. If you don’t modify the DecimalPlaces property (0 by default), 1.5 and 2.5 display as 2 and 3, respectively. We set the NumericUpDown’s ReadOnly property to true to indicate that the user cannot type a number into the control to make a selection. Thus, the user must click the up and down arrows to modify the value in the control. By default, the ReadOnly property is set to false. The output for this app is displayed in a multiline read-only TextBox with a vertical scrollbar, so the user can scroll through the entire output. Notice that is required (lines 24 and 31) to move to the next line in the TextBox.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset