Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.

Similar presentations


Presentation on theme: "Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes."— Presentation transcript:

1 Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes

2 Microsoft Visual Basic 2008: Reloaded, Third Edition2 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 InputBox function Refresh the screen

3 Microsoft Visual Basic 2008: Reloaded, Third Edition3 Objectives (continued) Delay program execution Enable and disable a control

4 Microsoft Visual Basic 2008: Reloaded, Third Edition4 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

5 Microsoft Visual Basic 2008: Reloaded, Third Edition5 The Repetition Structure (continued) Repetition statements in Visual Basic –Do...Loop –For...Next –For Each...Next

6 Microsoft Visual Basic 2008: Reloaded, Third Edition6 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

7 Microsoft Visual Basic 2008: Reloaded, Third Edition7 The Do...Loop Statement (continued) Figure 6-1: How to use the Do…Loop statement

8 Microsoft Visual Basic 2008: Reloaded, Third Edition8 The Do...Loop Statement (continued) Figure 6-1: How to use the Do…Loop statement (continued)

9 The Do...Loop Statement (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition9 Figure 6-2: Processing steps for the pretest loop example

10 Microsoft Visual Basic 2008: Reloaded, Third Edition10 The Do...Loop Statement (continued) Figure 6-3: Processing steps for the posttest loop example

11 Microsoft Visual Basic 2008: Reloaded, Third Edition11 The Do...Loop Statement (continued) Figure 6-4: Pseudocode and flowchart for the pretest loop example

12 Microsoft Visual Basic 2008: Reloaded, Third Edition12 The Do...Loop Statement (continued) Figure 6-5: Pseudocode and flowchart for the posttest loop example

13 Microsoft Visual Basic 2008: Reloaded, Third Edition13 The Do...Loop Statement (continued) Figure 6-6: Examples showing that the pretest and posttest loops do not always produce the same results

14 Microsoft Visual Basic 2008: Reloaded, Third Edition14 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

15 Microsoft Visual Basic 2008: Reloaded, Third Edition15 The InputBox Function Function: a predefined procedure that performs a specific task and returns a value 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

16 Microsoft Visual Basic 2008: Reloaded, Third Edition16 The InputBox Function (continued) Figure 6-7: Example of a dialog box created by the InputBox function

17 Microsoft Visual Basic 2008: Reloaded, Third Edition17 The InputBox Function (continued) InputBox function parameters: –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

18 Microsoft Visual Basic 2008: Reloaded, Third Edition18 The InputBox Function (continued) Figure 6-8: How to use the InputBox function

19 Microsoft Visual Basic 2008: Reloaded, Third Edition19 The InputBox Function (continued) Figure 6-8: How to use the InputBox function (continued)

20 Microsoft Visual Basic 2008: Reloaded, Third Edition20 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

21 Microsoft Visual Basic 2008: Reloaded, Third Edition21 The Sales Express Application (continued) Figure 6-9: Pseudocode for the Sales Express application

22 Microsoft Visual Basic 2008: Reloaded, Third Edition22 The Sales Express Application (continued) Figure 6-10: Sales Entry dialog box Figure 6-11: Average sales amount displayed in the interface

23 Microsoft Visual Basic 2008: Reloaded, Third Edition23 Figure 6-2: Code for the calcButton in the Sales Express application

24 Microsoft Visual Basic 2008: Reloaded, Third Edition24 Using 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

25 Microsoft Visual Basic 2008: Reloaded, Third Edition25 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

26 Microsoft Visual Basic 2008: Reloaded, Third Edition26 Adding Items to a List Box (continued) Figure 6-13: How to add items to a list box

27 Microsoft Visual Basic 2008: Reloaded, Third Edition27 Adding Items to a List Box (continued) Figure 6-14: Add methods entered in the form’s Load event procedure

28 Microsoft Visual Basic 2008: Reloaded, Third Edition28 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 animalListBox and codeListBox

29 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 Edition29

30 Accessing Items in a List Box (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition30 Figure 6-16: How to access an item in a list box

31 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 Edition31

32 Microsoft Visual Basic 2008: Reloaded, Third Edition32 Figure 6-17: How to determine the number of items in a list box

33 Microsoft Visual Basic 2008: Reloaded, Third Edition33 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

34 Microsoft Visual Basic 2008: Reloaded, Third Edition34 The SelectedItem and SelectedIndex Properties (continued) Figure 6-18: Item selected in the animalListBox

35 Microsoft Visual Basic 2008: Reloaded, Third Edition35 Figure 6-19: How to use the SelectedItem and SelectedIndex properties

36 Microsoft Visual Basic 2008: Reloaded, Third Edition36 The SelectedItem and SelectedIndex Properties (continued) Figure 6-20: How to select the default list box item

37 Microsoft Visual Basic 2008: Reloaded, Third Edition37 The SelectedItem and SelectedIndex Properties (continued) Figure 6-21: Code to select the default item in each list box

38 Microsoft Visual Basic 2008: Reloaded, Third Edition38 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

39 Microsoft Visual Basic 2008: Reloaded, Third Edition39 The SelectedValueChanged and SelectedIndexChanged Events (continued) Figure 6-22: SelectedValueChanged and SelectedIndexChanged event procedures

40 The SelectedValueChanged and SelectedIndexChanged Events (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition40 Figure 6-23: Result of processing the SelectedValueChanged and SelectedIndexChanged event procedures

41 Microsoft Visual Basic 2008: Reloaded, Third Edition41 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

42 Microsoft Visual Basic 2008: Reloaded, Third Edition42 The Product Finder Application (continued) Figure 6-24: Pseudocode for the Product Finder application

43 Microsoft Visual Basic 2008: Reloaded, Third Edition43 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

44 Microsoft Visual Basic 2008: Reloaded, Third Edition44 Figure 6-27: Code for the form’s Load event and findButton ’s Click event procedures

45 Microsoft Visual Basic 2008: Reloaded, Third Edition45 Programming Tutorial Figure 6-29: User interface Creating the Roll ‘Em Game Sleep method: delays execution of the program Refresh method: redraws the user interface

46 Microsoft Visual Basic 2008: Reloaded, Third Edition46 Programming Example Grade Calculator Figure 6-43: User interface

47 Microsoft Visual Basic 2008: Reloaded, Third Edition47 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

48 Microsoft Visual Basic 2008: Reloaded, Third Edition48 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, totals, and averages InputBox function: allows user input Verify that a variable does not contain a value of 0 before using it as a divisor

49 Microsoft Visual Basic 2008: Reloaded, Third Edition49 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 SelectedItem property of a list box: contains the value of the selected item in the list

50 Microsoft Visual Basic 2008: Reloaded, Third Edition50 Summary (continued) 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 Sleep method: delays program execution Me.Refresh : refreshes (redraws) the form Enabled property: used to enable or disable a control


Download ppt "Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes."

Similar presentations


Ads by Google