microsoft visual basic 2010: reloaded fourth edition chapter seven more on the repetition structure
TRANSCRIPT
Objectives
After studying this chapter, you should be able to:
• Code a counter-controlled loop using the For…Next statement
• Nest repetition structures
• Calculate a periodic payment using the Financial.Pmt method
• Select the existing text in a text box
• Code the TextChanged event procedure for a text box
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 2
Objectives (cont'd.)
• Code a list box’s SelectedValueChanged and SelectedIndexChanged event procedures
• Include a combo box in an interface
• Code the TextChanged event procedure for a combo box
• Store images in an image list control
• Display an image stored in an image list control
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 3
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
The For...Next Statement
• For...Next statement: processes a set of instructions a known number of times– Is a pretest loop
• Counter-controlled loop: a loop whose processing is controlled by a counter
• Counter variable: used to keep track of the number of times the loop has been processed
• Startvalue, endvalue, and stepvalue items– Control the number of times the loop is processed– Must evaluate to numeric values– Can be positive or negative
4
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
The For...Next Statement (cont'd.)
• A negative stepvalue causes the loop counter to count down
• Decrementing: adding increments of a negative number
• Flowchart symbol for the For...Next loop is a hexagon
• Values for the counter variable, startvalue, stepvalue, and endvalue are shown within the hexagon
5
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 6
Figure 7-1: How to use the For…Next statement
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 7
Figure 7-1: How to use the For…Next statement (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
The For...Next Statement (cont'd.)
8
Figure 7-2: Processing steps for Example 1 in Figure 7-1
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
The For...Next Statement (cont'd.)
9
Figure 7-2: Processing steps for Example 1 in Figure 7-1 (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 10
Figure 7-3: Problem specification for the Savings Account application
The For...Next Statement (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 11
Figure 7-4: Sample run of the Savings Account application
The For...Next Statement (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 12
Figure 7-5: Pseudocode and flowchart for the Calculate button’s Click event procedure
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 13
Figure 7-6: Calculate button’s Click event procedure
The For...Next Statement (cont'd.)
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
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 15
Figure 7-7: Comparison of the For…Next and Do…Loop statements
The For...Next Statement (cont'd.)
Nested Repetition Structures
• Repetition structures can be nested– Can use pretest or posttest loops for outer loop and
for inner (nested) loop
• A clock with minute and second hands demonstrates nested loops– Minute hand moves 1 position, then the second hand
moves 60 positions
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 7-8: Three versions of the logic used by a clock’s minute and second hands
Nested Repetition Structures (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 7-9: Modified problem specification for the Savings Account application
Figure 7-9: Sample run of the Savings Account application
Nested Repetition Structures (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 7-11: Two versions of the Calculate button’s Click event procedure
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 7-11: Two versions of the Calculate button’s Click event procedure (cont’d.)
The Payment Calculator Application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 7-12: Problem specification for the Payment Calculator application
The Payment Calculator Application (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 7-13: Sample run of the Payment Calculator application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
The Financial.Pmt Method
• Financial.Pmt method – Calculates a periodic payment on a loan or investment– Returns the periodic payment as a Double type value
• Rate and number of periods arguments must be expressed in the same units (monthly, annual, etc.)
23
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 24
Figure 7-14: How to use the Financial.Pmt method
The Financial.Pmt Method (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 7-15: Pseudocode and code for the Calculate button’s Click event procedure
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 7-15: Pseudocode and code for the Calculate button’s Click event procedure
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
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 27
Selecting the Existing Text in a Text Box (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 28
Figure 7-16: How to use the SelectAll method
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 29
Figure 7-17: Loan text box’s Enter event procedure
Selecting the Existing Text in a Text Box (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 30
Figure 7-18: Result of processing the text box’s Enter event procedure
Selecting the Existing Text in a Text Box (cont'd.)
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
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 31
Figure 7-19: New loan amount entered in the Loan text box
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
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 32
Figure 7-20: New principal entered in the Principal text box
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 33
Figure 7-21: Result of processing the text box’s TextChanged event procedure
Coding the TextChanged Event Procedure (cont’d.)
Coding the SelectedValueChanged and SelectedIndexChanged Event
Procedures
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 34
Figure 7-22: The list box’s SelectedValueChanged and SelectedIndexChanged event procedures
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
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 35
Using a Combo Box in an Interface
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 36
Figure 7-23: Examples of the three styles of combo boxes
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 37
Figure 7-24: Code used to fill the combo boxes in Figure 7-23 with values
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
38Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Using a Combo Box in an Interface (cont'd.)
39
Figure 7-25: Sample run of the Payment Calculator application using a combo box
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
40
Figure 7-26: Code for the Payment Calculator application shown in Figure 7-25Microsoft Visual Basic 2010: Reloaded, Fourth Edition
41
Figure 7-26: Code for the Payment Calculator application shown in Figure 7-25 (cont’d.)Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Using an Image List Control (cont’d.)
Figure 7-27: Image Viewer application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Using an Image List Control (cont’d.)
Figure 7-28: How to add images to an image list control’s Images collection
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Using an Image List Control (cont’d.)
Figure 7-29: Completed Images Collection Editor window in the Image Viewer application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Using an Image List Control (cont’d.)
Figure 7-30: How to refer to an image in the Images collection
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Using an Image List Control (cont’d.)
Figure 7-31: Sample run of the Image Viewer application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Using an Image List Control (cont’d.)
Figure 7-32: Code entered in the View button’s Click event procedure
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
• Creating the Slot Machine Application
Programming Tutorial 1
49
Figure 7-34: MainForm in the Slot Machine application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
• Creating the Birmingham Sales Application
Programming Tutorial 2
50
Figure 7-40: MainForm for the Birmingham Sales application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Programming Example
51
• Raise Calculator Application
Figure 7-46: MainForm for the Raise Calculator application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Summary
• For...Next statement: a pretest loop that will process the instructions a fixed number of times
• A variable declared in a For clause has block scope
• Use a hexagon in a flowchart for a For…Next loop• Financial.Pmt method: calculates a periodic
payment on a loan or investment• SelectAll method: highlights text in a text box• TextChanged event: occurs when a control’s text
changes
52Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Summary (cont'd.)
• Combo box: similar to a list box but may not expose the list items until clicked
• Three styles of combo boxes: Simple, DropDown, and DropDownList
• Combo box’s Items.Add method: adds an item to the list
• Use SelectedItem, SelectedIndex, or Text property of a combo box to set the default item in a combo
• Combo box’s Items.Count property: used to obtain the number of items listed in the combo box
53Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Summary (cont'd.)
• Combo box’s Sorted property: used to sort items listed in a combo box
• Combo box’s SelectedItem property: contains the value of the item selected in the list portion of the combo box
• Combo box’s Text property: contains the value that appears in the text portion of the combo box
• Combo box’s TextChanged event: occurs when the user either selects an item in the list portion or types a value in the text portion
54Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Summary (cont'd.)
• Image list control: stores a set of images in its Images collection
• Images in an image list control belong to the Images collection
• Access an image in the Images collection using the Item property with the index of the desired image
• Image collection’s Count property contains the number of images in the collection
• Use a picture box control to display an image contained in an image list control
55Microsoft Visual Basic 2010: Reloaded, Fourth Edition