Microsoft Visual Basic 2008 Reloaded Third Edition Chapter
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes
Objectives After studying this chapter, you should be able to: • Include the Do loop in both pseudocode and a flowchart • Write a Do…Loop statement • Initialize counters and accumulators • Display a dialog box using the Input. Box function • Refresh the screen Microsoft Visual Basic 2008: Reloaded, Third Edition 2
Objectives (continued) • Delay program execution • Enable and disable a control Microsoft Visual Basic 2008: Reloaded, Third Edition 3
The Repetition Structure • Repetition structure (or loop): a structure that repeatedly processes one or more program instructions until a condition is met • 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 2008: Reloaded, Third Edition 4
The Repetition Structure (continued) • Repetition statements in Visual Basic – Do. . . Loop – For. . . Next – For Each. . . Next Microsoft Visual Basic 2008: Reloaded, Third Edition 5
The Do. . . Loop Statement • Do. . . Loop statement: codes both a pretest or posttest loop • Use While or Until to code the condition for the loop • Repetition symbol in a flowchart is the diamond Microsoft Visual Basic 2008: Reloaded, Third Edition 6
The Do. . . Loop Statement (continued) Figure 6 -1: How to use the Do…Loop statement Microsoft Visual Basic 2008: Reloaded, Third Edition 7
The Do. . . Loop Statement (continued) Figure 6 -1: How to use the Do…Loop statement (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 8
The Do. . . Loop Statement (continued) Figure 6 -2: Processing steps for the pretest loop example Microsoft Visual Basic 2008: Reloaded, Third Edition 9
The Do. . . Loop Statement (continued) Figure 6 -3: Processing steps for the posttest loop example Microsoft Visual Basic 2008: Reloaded, Third Edition 10
The Do. . . Loop Statement (continued) Figure 6 -4: Pseudocode and flowchart for the pretest loop example Microsoft Visual Basic 2008: Reloaded, Third Edition 11
The Do. . . Loop Statement (continued) Figure 6 -5: Pseudocode and flowchart for the posttest loop example Microsoft Visual Basic 2008: Reloaded, Third Edition 12
The Do. . . Loop Statement (continued) Figure 6 -6: Examples showing that the pretest and posttest loops do not always produce the same results Microsoft Visual Basic 2008: Reloaded, Third Edition 13
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 2008: Reloaded, Third Edition 14
The Input. Box Function • Function: a predefined procedure that performs a specific task and returns a value • Input. Box 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 • Input. Box 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 2008: Reloaded, Third Edition 15
The Input. Box Function (continued) Figure 6 -7: Example of a dialog box created by the Input. Box function Microsoft Visual Basic 2008: Reloaded, Third Edition 16
The Input. Box Function (continued) • Input. Box function parameters: – prompt: the message to display inside the dialog box – title: the text to display in the dialog box’s title bar – default. Response: a prefilled value for the user input Microsoft Visual Basic 2008: Reloaded, Third Edition 17
The Input. Box Function (continued) Figure 6 -8: How to use the Input. Box function Microsoft Visual Basic 2008: Reloaded, Third Edition 18
The Input. Box Function (continued) Figure 6 -8: How to use the Input. Box function (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 19
The Sales Express Application • Requirements: display the average amount the company sold during the prior year • Input: the amount of each salesperson’s sales • Priming read: used to obtain the first input • Must verify that a variable does not contain the value 0 before using it as a divisor Microsoft Visual Basic 2008: Reloaded, Third Edition 20
The Sales Express Application (continued) Figure 6 -9: Pseudocode for the Sales Express application Microsoft Visual Basic 2008: Reloaded, Third Edition 21
The Sales Express Application (continued) Figure 6 -10: Sales Entry dialog box Figure 6 -11: Average sales amount displayed in the interface Microsoft Visual Basic 2008: Reloaded, Third Edition 22
Figure 6 -2: Code for the calc. Button in the Sales Express application Microsoft Visual Basic 2008: Reloaded, Third Edition 23
Using a List Box in an Interface • List box: displays a list of choices from which the user can select zero or more choices • Selection. Mode property: controls the number of choices a user can select – None: user can scroll but not select anything – One: user can select one item – Multi. Simple and Multi. Extended: user can select multiple items Microsoft Visual Basic 2008: Reloaded, Third Edition 24
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 • Load event of a form: occurs when an application is started and the form is displayed for the first time Microsoft Visual Basic 2008: Reloaded, Third Edition 25
Adding Items to a List Box (continued) Figure 6 -13: How to add items to a list box Microsoft Visual Basic 2008: Reloaded, Third Edition 26
Adding Items to a List Box (continued) Figure 6 -14: Add methods entered in the form’s Load event procedure Microsoft Visual Basic 2008: Reloaded, Third Edition 27
Adding Items to a List Box (continued) • Sorted property: – Determines if the list box items are sorted – Sort order is dictionary order Figure 6 -15: Items added to the animal. List. Box and code. List. Box Microsoft Visual Basic 2008: Reloaded, Third Edition 28
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 2008: Reloaded, Third Edition 29
Accessing Items in a List Box (continued) Figure 6 -16: How to access an item in a list box Microsoft Visual Basic 2008: Reloaded, Third Edition 30
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 2008: Reloaded, Third Edition 31
Figure 6 -17: How to determine the number of items in a list box Microsoft Visual Basic 2008: Reloaded, Third Edition 32
The Selected. Item and Selected. Index Properties • Selected. Item property: – Contains the value of the selected item in the list – If nothing is selected, it contains the empty string • Selected. Index 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 2008: Reloaded, Third Edition 33
The Selected. Item and Selected. Index Properties (continued) Figure 6 -18: Item selected in the animal. List. Box Microsoft Visual Basic 2008: Reloaded, Third Edition 34
Figure 6 -19: How to use the Selected. Item and Selected. Index properties Microsoft Visual Basic 2008: Reloaded, Third Edition 35
The Selected. Item and Selected. Index Properties (continued) Figure 6 -20: How to select the default list box item Microsoft Visual Basic 2008: Reloaded, Third Edition 36
The Selected. Item and Selected. Index Properties (continued) Figure 6 -21: Code to select the default item in each list box Microsoft Visual Basic 2008: Reloaded, Third Edition 37
The Selected. Value. Changed and Selected. Index. Changed Events • Selected. Value. Changed and Selected. Index. Changed 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 2008: Reloaded, Third Edition 38
The Selected. Value. Changed and Selected. Index. Changed Events (continued) Figure 6 -22: Selected. Value. Changed and Selected. Index. Changed event procedures Microsoft Visual Basic 2008: Reloaded, Third Edition 39
The Selected. Value. Changed and Selected. Index. Changed Events (continued) Figure 6 -23: Result of processing the Selected. Value. Changed and Selected. Index. Changed event procedures Microsoft Visual Basic 2008: Reloaded, Third Edition 40
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 Microsoft Visual Basic 2008: Reloaded, Third Edition 41
The Product Finder Application (continued) Figure 6 -24: Pseudocode for the Product Finder application Microsoft Visual Basic 2008: Reloaded, Third Edition 42
The Product Finder Application (continued) Figure 6 -25: Sample run of the application when an ID is found Figure 6 -26: Sample run of the application when an ID is not found Microsoft Visual Basic 2008: Reloaded, Third Edition 43
Figure 6 -27: Code for the form’s Load event and find. Button’s Click event procedures Microsoft Visual Basic 2008: Reloaded, Third Edition 44
Programming Tutorial • Creating the Roll ‘Em Game • Sleep method: delays execution of the program • Refresh method: redraws the user interface Figure 6 -29: User interface Microsoft Visual Basic 2008: Reloaded, Third Edition 45
Programming Example • Grade Calculator Figure 6 -43: User interface Microsoft Visual Basic 2008: Reloaded, Third Edition 46
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 Microsoft Visual Basic 2008: Reloaded, Third Edition 47
Summary (continued) • Do. . . Loop statement: codes a pretest or posttest loop • Use a While or Until condition in a Do. . . Loop • Flowchart symbol for repetition is a diamond • Counter and accumulators: variables that calculate subtotals, and averages • Input. Box function: allows user input • Verify that a variable does not contain a value of 0 before using it as a divisor Microsoft Visual Basic 2008: Reloaded, Third Edition 48
Summary (continued) • List box: contains a minimum of three selections • List box’s Items collection: contains the items in the list box • Items. Add method: adds an item to the list • 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 • Selected. Item property of a list box: contains the value of the selected item in the list Microsoft Visual Basic 2008: Reloaded, Third Edition 49
Summary (continued) • Selected. Index property of a list box: contains the index position of the selected item in the list • Selected. Value. Changed and Selected. Index. Changed events occur when an item in a list box is selected • Sleep method: delays program execution • Me. Refresh: refreshes (redraws) the form • Enabled property: used to enable or disable a control Microsoft Visual Basic 2008: Reloaded, Third Edition 50
- Slides: 50