Microsoft Visual Basic 2010: Reloaded: Chapter Six Repeating Program Instructions

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 67

Microsoft Visual Basic 2010:

Reloaded
Fourth Edition

Chapter Six
Repeating Program Instructions
Objectives

After studying this chapter, you should be able to:


• Differentiate between a pretest and a posttest loop
• Include pretest and posttest loops in pseudocode
and in a flowchart
• Write a Do…Loop statement
• Utilize counters and accumulators
• Display a dialog box using the InputBox function

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 2


Objectives (cont'd.)

• Include a list box in an interface


• Enable and disable a control
• Refresh the screen
• Delay program execution

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 3


The Repetition Structure
• Repetition structure (or loop): a structure that
repeatedly processes one or more program
instructions until a condition is met
• Looping condition: the requirement for repeating
the instructions
• Loop exit condition: the requirement for not
repeating the instructions

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 4


The Repetition Structure (cont’d.)

Figure 6-1: Example of a looping condition and a loop exit condition

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 5


The Repetition Structure (cont’d.)

Figure 6-2: Problem specification for the Getting to a Million Club application

Figure 6-3: Sample run of the Getting to a Million Club application


Microsoft Visual Basic 2010: Reloaded, Fourth Edition 6
The Repetition Structure (cont’d.)

Figure 6-4: Pseudocode containing only the sequence structure

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 7


The Repetition Structure (cont’d.)
• Pretest loop
– The condition is evaluated before the instructions
within the loop are processed
– The instructions may be processed zero or more
times
• Posttest loop
– The condition is evaluated after the instructions within
the loop are processed
– The instructions are always processed at least once

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 8


Figure 6-5: Modified problem specification and pseudocode containing a loop
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 9
The Repetition Structure (cont'd.)

• Repetition statements in Visual Basic


– Do...Loop
– For...Next
– For Each...Next

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 10


The Do...Loop Statement
• Do...Loop statement: codes both a pretest and
posttest loop
• Loop body: the instructions between the Do and the
Loop clauses
• Use While or Until to code the condition for the loop
• Repetition symbol in a flowchart is the diamond

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 11


Figure 6-6: How to use the Do…Loop statement
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 12
The Do...Loop Statement (cont'd.)

Figure 6-7: Flowchart for the pretest loop example from Figure 6-6
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 13
The Do...Loop Statement (cont'd.)

Figure 6-8: Flowchart for the posttest loop example from Figure 6-6
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 14
Using Counters and Accumulators

• Counter: a numeric variable used for counting


• Accumulator: a numeric variable used for
accumulating (adding together)
• Initializing: assigning a beginning value to a
counter or accumulator variable
• Updating (or incrementing): adding a number to
the value of a counter or accumulator variable
• Counters are always incremented by a constant
value, usually 1

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 15


Figure 6-9: Modified pseudocode and code for the calcButton’s Click event
procedure
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 16
Using Counters and Accumulators
(cont’d.)

Figure 6-9: Modified pseudocode and code for the calcButton’s Click event
procedure (cont’d.)

Figure 6-10: Sample run of the modified Getting to a Million Club application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 17
The Sales Express Company
Application
• Requirements: display the average amount the
company sold during the prior year
• Input: the amount of each salesperson’s sales
• Multiline property: if True, allows multiple lines
of text in a text box
• ReadOnly property: if True, prevents a user from
changing the text box contents during run time
• ScrollBars property: specifies whether a text
box has no scroll bars, horizontal or vertical, or
both

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 18


The Sales Express Company
Application (cont'd.)

Figure 6-11: Problem specification for the Sales Express Company


application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 19


The Sales Express Company
Application (cont'd.)

Figure 6-12: Sample run of the Sales Express Company application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 20


The Sales Express Company
Application (cont'd.)

Figure 6-13: Pseudocode for the Average button’s Click event procedure

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 21


The Sales Express Company
Application (cont'd.)
• Priming read: used to obtain the first input
• Update read: allows the user to update the value
of an input item
• Infinite (or endless) loop: a loop that has no way
to end
• Must verify that a variable does not contain the
value 0 before using it as a divisor

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 22


Figure 6-14: Flowchart for the Average button’s Click event procedure
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 23
The InputBox Function

• InputBox function: displays a predefined dialog


box that allows the user to enter data
– Contains a text message, an OK button, a Cancel
button, and an input area
• InputBox function returns:
– The user’s entry if the user clicks the OK button
– An empty string if the user clicks the Cancel button
or the Close button on the title bar

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 24


The InputBox Function (cont'd.)

Figure 6-15: Example of an input dialog box

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 25


The InputBox Function (cont'd.)

• InputBox function arguments:


– prompt: the message to display inside the dialog box
– title: the text to display in the dialog box’s title bar
– defaultResponse: a prefilled value for the user input

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 26


Figure 6-16: How
to use the
InputBox
function

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 27


The InputBox Function (cont'd.)

Figure 6-16: How to use the InputBox function (cont'd.)


Microsoft Visual Basic 2010: Reloaded, Fourth Edition 28
Figure 6-17: Code associated with the pseudocode and flowchart shown in
Figures 6-13 and 6-14, respectively
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 29
The InputBox Function (cont'd.)

Figure 6-17: Code associated with the pseudocode and flowchart shown in
Figures 6-13 and 6-14, respectively (cont’d.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 30


Including a List Box in an Interface

• List box: displays a list of choices from which the


user can select zero or more choices
• SelectionMode property: controls the number of
choices a user can select
– None: user can scroll but not select anything
– One: user can select one item
– MultiSimple and MultiExtended: user can select
multiple items

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 31


Adding Items to a List Box
• Items collection: a collection of the items in a list
box
• Collection: a group of one or more individual objects
treated as one unit
• Add method: adds an item to the list box’s Items
collection
– Items to be added must be converted to String
– String Collection Editor window can be used to
specify list items during design time
• Load event of a form: occurs when an application is
started and the form is displayed for the first time
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 32
Adding Items to a List Box (cont'd.)

Figure 6-18: String Collection Editor window

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 33


Adding Items to a List Box (cont'd.)

Figure 6-19: How to use the Items collection’s Add method


Microsoft Visual Basic 2010: Reloaded, Fourth Edition 34
Adding Items to a List Box (cont'd.)

Figure 6-20: Add methods entered in the MainForm’s Load event procedure
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 35
Adding Items to a List Box (cont'd.)

Figure 6-21: Result of processing the Add methods in Figure 6-20

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 36


Adding Items to a List Box (cont'd.)

Figure 6-22: Sample run of the Jasper’s Food Hut application

Figure 6-23: Add to List button’s Click event procedure in the Jasper’s
Food Hut application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 37
Adding Items to a List Box (cont'd.)

Figure 6-24: Sample run of the Clark’s Chicken application

Figure 6-25: Add to List button’s Click event procedure in the Clark’s
Chicken application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 38
Adding Items to a List Box (cont'd.)
• Sorted property:
– Determines if the list box items are sorted
– Sort order is dictionary order

Figure 6-26: Examples of the list box’s Sorted property


Microsoft Visual Basic 2010: Reloaded, Fourth Edition 39
Accessing Items in a List Box

• Index:
– A unique number that identifies an item in a
collection
– Is zero-relative: the first item has index of 0

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 40


Accessing Items in a List Box (cont'd.)

Figure 6-27: How to access an item in a list box


Microsoft Visual Basic 2010: Reloaded, Fourth Edition 41
Determining the Number of Items
in a List Box
• Items.Count property: stores the number of
items in a list box
– Count value is always one higher than the highest
index in the list box

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 42


Figure 6-28: How to determine the number of items in a list box
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 43
The SelectedItem and
SelectedIndex Properties
• SelectedItem property:
– Contains the value of the selected item in the list
– If nothing is selected, it contains the empty string
• SelectedIndex property:
– Contains the index of the selected item in the list
– If nothing is selected, it contains the value -1
• Default list box item: the item that is selected by
default when the interface first appears

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 44


The SelectedItem and
SelectedIndex Properties (cont'd.)

Figure 6-29: Item selected in the animalListBox

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 45


Figure 6-30: How to use
the SelectedItem and
SelectedIndex
properties

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 46


The SelectedItem and
SelectedIndex Properties (cont'd.)

Figure 6-31: How to select the default list box item

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 47


The SelectedValueChanged and
SelectedIndexChanged Events

• SelectedValueChanged and
SelectedIndexChanged events:
– Occur when a user selects an item in a list box
– Occur when a code statement selects an item in a list
box

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 48


The SelectedValueChanged and
SelectedIndexChanged Events
(cont’d.)

Figure 6-32: Code showing the SelectedValueChanged and


SelectedIndexChanged event procedures
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 49
The SelectedValueChanged and
SelectedIndexChanged Events
(cont’d.)

Figure 6-32: Code showing the SelectedValueChanged and


SelectedIndexChanged event procedures (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 50
The SelectedValueChanged and
SelectedIndexChanged Events
(cont’d.)

Figure 6-32: Code showing the SelectedValueChanged and


SelectedIndexChanged event procedures (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 51
The SelectedValueChanged and
SelectedIndexChanged Events
(cont’d.)

Figure 6-33: Result of processing the code shown in Figure 6-32

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 52


The Product Finder Application

• Allows the user to enter a product ID


• Searches for the ID in a list box
• If found, highlights the ID
• If not found, ensures that no ID is highlighted in the
list box

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 53


Figure 6-34: Sample run of the application when the produce ID is found

Figure 6-35: Sample run of the application when the product ID is not found
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 54
The Product Finder Application
(cont'd.)

Figure 6-36: Pseudocode for the Find button’s Click event procedure

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 55


Figure 6-37: Product Finder application’s code
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 56
Figure 6-37:
Product Finder
application’s code
(cont’d.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 57


The Color Viewer Application
• Enabled property: used to enable or disable a
control
– When False, the control appears dimmed (grayed
out), indicating it is not available for use
• Refresh method: ensures that the computer
processes any previous lines of code that affect the
interface’s appearance
• Sleep method: delays program execution
– Argument is specified in milliseconds

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 58


The Color Viewer Application (cont’d.)

Figure 6-38: MainForm in the Color Viewer application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 59


The Color Viewer Application (cont’d.)

Figure 6-39: View Colors button’s Click event procedure


Microsoft Visual Basic 2010: Reloaded, Fourth Edition 60
Programming Tutorial 1

• Creating the Roll ‘Em Game

Figure 6-41: MainForm for the Roll ‘Em Game application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 61


Programming Tutorial 2

• Coding the Just Birthdays Application

Figure 6-52: MainForm for the Just Birthdays application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 62


Programming Example

• Grade Calculator Application

Figure 6-63: MainForm in the Grade Calculator application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 63


Summary

• The three programming structures are sequence,


selection, and repetition
• Repetition structure (or loop): repeatedly processes
a set of instructions
• Pretest loop tests the condition before the
instructions are processed
• Posttest loop tests the condition after the
instructions are processed
• Do...Loop statement: codes a pretest or posttest
loop
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 64
Summary (cont'd.)

• Use a While or Until condition in a Do...Loop


• Flowchart symbol for repetition is a diamond
• Counter and accumulators: variables that calculate
subtotals, totals, and averages
• Priming read gets the first value prior to the loop
• Update read gets remaining values from the user
• InputBox function: allows user input
• Verify that a variable does not contain a value of 0
before using it as a divisor
• List box: displays a list of choices
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 65
Summary (cont'd.)

• List box’s Items collection: contains the items in the


list box
• Use String Collection Editor window to add list
items during design time
• Items.Add method: adds an item to the list during
run time
• Form’s Load event occurs before the form appears
• List box item’s index is used to access the item
• Items.Count property stores the number of items

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 66


Summary (cont'd.)
• SelectedItem property of a list box: contains the
value of the selected item in the list
• SelectedIndex property of a list box: contains
the index position of the selected item in the list
• SelectedValueChanged and
SelectedIndexChanged events occur when an
item in a list box is selected
• Enabled property: enables or disables a control
• Sleep method: delays program execution
• Me.Refresh: refreshes (redraws) the form
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 67

You might also like