670 likes | 686 Views
Learn how to differentiate pretest and posttest loops, write Do…Loop statements, utilize counters and accumulators, and more in Visual Basic 2010.
E N D
Microsoft Visual Basic 2010: ReloadedFourth 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
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
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
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
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
The Repetition Structure (cont’d.) Figure 6-4: Pseudocode containing only the sequence structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
Figure 6-5: Modified problem specification and pseudocode containing a loop Microsoft Visual Basic 2010: Reloaded, Fourth Edition
The Repetition Structure (cont'd.) • Repetition statements in Visual Basic • Do...Loop • For...Next • For Each...Next Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
Figure 6-6: How to use the Do…Loop statement Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
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
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
Figure 6-9: Modified pseudocode and code for the calcButton’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
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
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
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
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
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
Figure 6-14: Flowchart for the Average button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
The InputBox Function (cont'd.) Figure 6-15: Example of an input dialog box Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
Figure 6-16: How to use the InputBox function Microsoft Visual Basic 2010: Reloaded, Fourth Edition
The InputBox Function (cont'd.) Figure 6-16: How to use the InputBox function (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
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
Including a List Box in an Interface • List box: displays a list of choices from which the user can select zero or more choices • SelectionModeproperty: 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
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
Adding Items to a List Box (cont'd.) Figure 6-18: String Collection Editor window Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
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
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
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
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
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
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
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
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
Figure 6-28: How to determine the number of items in a list box Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
The SelectedItem and SelectedIndex Properties (cont'd.) Figure 6-29: Item selected in the animalListBox Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 6-30: How to use the SelectedItem and SelectedIndex properties Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
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
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
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