0% found this document useful (0 votes)
48 views

VB 07

1. The document discusses an interest calculator application that uses a For...Next loop to calculate the value of an investment over a specified number of years. 2. It explains how to use NumericUpDown controls to allow the user to select the number of years, and a multiline text box to display the results. 3. The code example shows how to retrieve the user input, perform the calculations in a For...Next loop, and display the output in the text box.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

VB 07

1. The document discusses an interest calculator application that uses a For...Next loop to calculate the value of an investment over a specified number of years. 2. It explains how to use NumericUpDown controls to allow the user to select the number of years, and a multiline text box to display the results. 3. The code example shows how to retrieve the user input, perform the calculations in a For...Next loop, and display the output in the text box.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Microsoft Visual Basic:

Reloaded

Chapter Seven
More on the Repetition Structure
Overview

■ Introducing the Interest Calculator Application


■ For...Next repetition statements
■ Select the existing text in a text box
■ Code the TextChanged event procedure
■ Code a list box’s SelectedValueChanged and
SelectedIndexChanged event procedures
■ Code the TextChanged event procedure for a
combo box
■ Store images in an image list control

 2009 Pearson Education, Inc. All rights reserved.


Interest Calculator Application
Application Requirements

You are investing $1,000.00 in a savings account that yields 5% interest


compounded annually. Assuming that you leave all interest on deposit,
calculate and display the amount of money in the account at the end of
each year over a period of n years. To compute these amounts, use the
following formula:
a = p (1 + r)n
where
p is the original amount of money invested (the principal)
r is the annual interest rate (for example, .05 is equivalent to 5%)
n is the number of years
a is the amount on deposit at the end of the nth year.

 2009 Pearson Education, Inc. All rights reserved.


Interest Calculator Application

Click to increase number of years

NumericUpDown control

Click to decrease number of years

Multiline TextBox displays


application results

Figure 11.2 | Output of completed Interest Calculator application.

 2009 Pearson Education, Inc. All rights reserved.


Designing the Interest Calculator Application

When the user clicks the Calculate Button


Get the values for the principal, interest rate and years
entered by the user
Store a header String to be added to the output TextBox

For each year (starting at 1 and ending with the number


of years entered)
Calculate the current value of the investment
Append the year and the current value of the
investment to the String that will be displayed in
the output TextBox
Display the final output in the output TextBox

 2009 Pearson Education, Inc. All rights reserved.


For...Next Repetition Statement

■ The For...Next repetition statement makes it easier for


you to write code to perform counter-controlled repetition.
■ It takes less time to code and is easier to read.
■ The For...Next header (Fig. 11.4) specifies all four
essential elements for counter-controlled repetition.

Figure 11.4 | For...Next header components.

 2009 Pearson Education, Inc. All rights reserved.


For...Next Repetition Statement

For counter As Integer = 2 To 10 Step 2


body statement(s)
Next
■ A For...Next statement begins with the keyword
For.
■ The statement declares and initializes a control
variable.
Note that you do not use the Dim keyword.
■ Following the initial value of the control variable is the
keyword To, followed by the final value of the control
variable.

 2009 Pearson Education, Inc. All rights reserved.


Examples Using the For...Next Statement

■ The following examples demonstrate different ways of


varying the control variable in a For...Next
statement.
Vary the control variable from 1 to 100 in increments of 1.
For i As Integer = 1 To 100
or
For i As Integer = 1 To 100 Step 1
Vary the control variable from 100 to 1 in increments of -1
(decrements of 1).
For i As Integer = 100 To 1 Step -1

 2009 Pearson Education, Inc. All rights reserved.


Examples Using the For...Next Statement

Vary the control variable from 7 to 77 in increments of 7.


For i As Integer = 7 To 77 Step 7
Vary the control variable from 20 to -20 in increments of
-2 (decrements of 2).
For i As Integer = 20 To -20 Step -2
Vary the control variable over the sequence of the following
values: 2, 5, 8, 11, 14, 17, 20.
For i As Integer = 2 To 20 Step 3
Vary the control variable over the sequence of the following
values: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0.
For i As Integer = 99 To 0 Step -11

 2009 Pearson Education, Inc. All rights reserved.


1 For years As Integer = 2 To 5
2 months = 12 * years ' calculate payment period
3
4 ' calculate payment value
5 value = Pmt(monthlyRate, months, -loanAmount)
6
7 ' display payment value
8 paymentsListBox.Items.Add(months & ControlChars.Tab & _
9 ControlChars.Tab & String.Format("{0:C}", value))
10 Next

 2009 Pearson Education, Inc. All rights reserved.


The For...Next Statement (cont'd.)

■ Can use For…Next or Do…Loop for a counter-


controlled loop
■ With Do…Loop, you must:
Declare and initialize the counter variable
Update the counter variable
Include an appropriate comparison in the Do clause
■ For…Next statement handles the declaration,
initialization, update, and comparison tasks
Is more convenient to use

 2009 Pearson Education, Inc. All rights reserved.


The For...Next Statement (cont'd.)

Figure 7-7: Comparison of the For…Next and Do…Loop statements

 2009 Pearson Education, Inc. All rights reserved.


NumericUpDown Controls

NumericUpDown
control

Figure 11.9 | NumericUpDown control added to Interest Calculator application.


 2009 Pearson Education, Inc. All rights reserved.
NumericUpDown Controls

■ By default, this control sets 0 as the minimum and


100 as the maximum.
To change these values, set the Maximum property of the Years:
NumericUpDown control to 10.
Then set its Minimum property to 1.
If the user inputs a value less than Minimum or greater than
Maximum, the value is automatically set to the minimum or
maximum value.
■ The Increment property specifies by how much the
current number in the NumericUpDown control
changes when the user clicks the control’s up or down
arrow.
This application uses the default value, 1.

 2009 Pearson Education, Inc. All rights reserved.


Adding and Customizing a Multiline
TextBox with a Scrollbar

■ Add a TextBox named resultTextBox (Fig. 11.10).


Change the TextBox’s Multiline property value to True.
Set the ReadOnly property to True.

Multiline TextBox Vertical scrollbar (disabled)

Figure 11.10 | Multiline TextBox with vertical scrollbar added to the Form.

 2009 Pearson Education, Inc. All rights reserved.


GUI Design Tips
■ If a TextBox will display multiple lines of output,
set the Multiline property to True and left align
the output by setting the TextAlign property to
Left.
■ If a multiline TextBox will display many lines of
output, limit the TextBox height and use a vertical
scrollbar to allow users to view additional lines of
output.
■ If a TextBox is used to display output, set the
ReadOnly property to True to ensure that the user
cannot change the output.

 2009 Pearson Education, Inc. All rights reserved.


1 Public Class InterestCalculatorForm
2 ' handles Calculate Button's Click event
3 Private Sub calculateButton_Click(ByVal sender As System.Object, _
4 ByVal e As System.EventArgs) Handles calculateButton.Click
5
6 ' declare variables to store user input
7 Dim principal As Decimal ' store principal
8 Dim rate As Double ' store interest rate
9 Dim amount As Decimal ' store each calculation
10 Dim output As String ' store output
11
12 ' retrieve user input
13 principal = Val(principalTextBox.Text)
14 rate = Val(rateTextBox.Text)
15

 2009 Pearson Education, Inc. All rights reserved.


16 ' set output header
17 output = "Year" & ControlChars.Tab _
18 & "Amount on Deposit" & ControlChars.CrLf
19
20 ' calculate amount after each year and append to string
21 For yearCounter = 1 To yearUpDown.Value
22 amount = _
23 principal * ((1 + rate / 100) ^ yearCounter)
24 output &= (yearCounter & ControlChars.Tab & _
25 String.Format("{0:C}", amount) & ControlChars.CrLf)
26 Next
27
28 resultTextBox.Text = output ' display result
29 End Sub ' calculateButton_Click
30 End Class ' InterestCalculatorForm

 2009 Pearson Education, Inc. All rights reserved.


Selecting the Existing Text in a Text Box

■ SelectAll method: selects all text in a text box


for replacement typing
User only needs to type the new value
■ Enter event: occurs when the text box receives
the focus
When the user tabs to the control
When the user uses the control’s access key
When the Focus method sends the focus to the control
When the user clicks in the text box

 2009 Pearson Education, Inc. All rights reserved.


Selecting the Existing Text in a Text Box (cont’d.)

Figure 7-17: Loan text box’s Enter event procedure

 2009 Pearson Education, Inc. All rights reserved.


Selecting the Existing Text in a Text Box (cont’d.)

Figure 7-18: Result of processing the text box’s Enter event procedure

 2009 Pearson Education, Inc. All rights reserved.


Selecting the Existing Text in a Text Box (cont’d.)

■ Entering a new value in the Loan text box does


not update the monthly payments until the user
clicks the Calculate button
– May cause confusion

Figure 7-19: New loan


amount entered in the
Loan text box

 2009 Pearson Education, Inc. All rights reserved.


Coding the TextChanged Event Procedure

■ TextChanged event
– Occurs when a change is made in a control’s Text
property
– Change may be made by user or the program

 2009 Pearson Education, Inc. All rights reserved.


Coding the TextChanged Event Procedure

Figure 7-21: Result of processing the text box’s TextChanged event procedure

 2009 Pearson Education, Inc. All rights reserved.


Coding the SelectedValueChanged and
SelectedIndexChanged Event Procedures

Figure 7-22: The list box’s SelectedValueChanged


and SelectedIndexChanged event procedures

 2009 Pearson Education, Inc. All rights reserved.


Including a Combo Box in an Interface

■ Combo Box control:


Similar to a list box with a list of choices
List portion may be hidden
May contain a text field that allows the user to type an entry
that is not on the list
■ Three styles of combo boxes, controlled by the
DropDownStyle property:
Simple
DropDown (the default)
DropDownList

 2009 Pearson Education, Inc. All rights reserved.


Using a Combo Box in an Interface

Figure 7-23: Examples of the three styles of combo boxes

 2009 Pearson Education, Inc. All rights reserved.


Figure 7-24: Code used to fill the combo boxes in Figure 7-23 with values

 2009 Pearson Education, Inc. All rights reserved.


Using a Combo Box in an Interface (cont'd.)

■ Items.Add method: used to add items to a


combo box
■ SelectedItem property: contains the value of
the selected item in the list
■ Text property: contains the value that appears in
the text portion of the control (item selected or
typed in)
■ Items.count property: used to obtain the
number of items in the combo box
■ Sorted property: used to sort the items in the list

 2009 Pearson Education, Inc. All rights reserved.


Using a Combo Box in an Interface (cont'd.)

Figure 7-25: Sample run of the Payment Calculator application using a combo box

 2009 Pearson Education, Inc. All rights reserved.


Figure 7-26: Code for the Payment Calculator application shown in Figure 7-25

 2009 Pearson Education, Inc. All rights reserved.


Figure 7-26: Code for the Payment Calculator application shown in Figure 7-25
(cont’d.)

 2009 Pearson Education, Inc. All rights reserved.


Using an Image List Control

■ Image List control:


Stores a collection of images
Does not appear on the form; appears in the component tray
■ Add images to the control using the Images
Collection Editor window

 2009 Pearson Education, Inc. All rights reserved.


Using an Image List Control (cont’d.)

Figure 7-27: Image Viewer application

 2009 Pearson Education, Inc. All rights reserved.


Using an Image List Control (cont’d.)

Figure 7-29: Completed Images Collection Editor window


in the Image Viewer application

 2009 Pearson Education, Inc. All rights reserved.


Using an Image List Control (cont’d.)

Figure 7-30: How to refer to an image in the Images collection

 2009 Pearson Education, Inc. All rights reserved.


Using an Image List Control (cont’d.)

Figure 7-31: Sample run of the Image Viewer application

 2009 Pearson Education, Inc. All rights reserved.


Using an Image List Control (cont’d.)

Figure 7-32: Code entered in the View button’s Click event procedure

 2009 Pearson Education, Inc. All rights reserved.

You might also like