Visual Basic 2005 for Programmers (2nd Edition)

13.10. NumericUpDown Control

At times, we will 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 sideone 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 number in the control changes when the user clicks the control's up and down arrows. Figure 13.35 describes common properties and a common event of class NumericUpDown.

Figure 13.35. NumericUpDown properties and an event.

NumericUpDown properties and an event

Description

Common Properties

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 to either the left or 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. It is the default event for the NumericUpDown control.

Figure 13.36 demonstrates using a NumericUpDown control for a GUI that calculates interest rate. The calculations performed in this application are similar to those performed in Fig. 6.8. 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.

Figure 13.36. Demonstrating the NumericUpDown control.

1 ' Fig. 13.36: FrmInterestCalculator.vb 2 ' Demonstrating the NumericUpDown control. 3 Public Class FrmInterestCalculator 4 5 Private Sub btnCalculate_Click(ByVal sender As System.Object, _ 6 ByVal e As System.EventArgs) Handles btnCalculate.Click 7 ' declare variables to store user input 8 Dim principal As Decimal ' store principal 9 Dim rate As Double ' store interest rate 10 Dim year As Integer ' store number of years 11 Dim amount As Decimal ' store amount 12 13 ' retrieve user input 14 principal = Convert.ToDecimal(txtPrincipal.Text) 15 rate = Convert.ToDouble(txtInterest.Text) 16 year = Convert.ToInt32(updYear.Value) 17 18 ' set output header 19 txtDisplay.Text = "Year" & vbTab & "Amount on Deposit" & vbCrLf 20 21 ' calculate amount after each year and append to output 22 For yearCounter As Integer = 1 To year 23 amount = principal * Convert.ToDecimal( _ 24 Math.Pow(1 + rate / 100, yearCounter)) 25 txtDisplay.Text &= (yearCounter & vbTab & _ 26 String.Format("{0:C}", amount) & vbCrLf) 27 Next 28 End Sub ' btnCalculate_Click 29 End Class ' FrmInterestCalculator

For the NumericUpDown control named updYear, we set the Minimum property to 1 and the Maximum property to 10. We left the Increment property's default value (1). 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. 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 (or use the arrow keys on the keyboard) to modify the value in the control. By default, the ReadOnly property is set to False. The output for this application is displayed in a multiline read-only TextBox with a vertical scrollbar so that the user can scroll through the entire output.

Категории