1 / 50

Microsoft Visual Basic 2008: Reloaded Third Edition

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

kcora
Télécharger la présentation

Microsoft Visual Basic 2008: Reloaded Third Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Microsoft Visual Basic 2008: ReloadedThird Edition Chapter Six The Do Loop and List Boxes

  2. 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 Microsoft Visual Basic 2008: Reloaded, Third Edition

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

  4. 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

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

  6. 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

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

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

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

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

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

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

  13. 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

  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 2008: Reloaded, Third Edition

  15. 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 Microsoft Visual Basic 2008: Reloaded, Third Edition

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

  17. 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 Microsoft Visual Basic 2008: Reloaded, Third Edition

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

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

  20. 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

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

  22. 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

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

  24. Using 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 2008: Reloaded, Third Edition

  25. 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

  26. 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

  27. 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

  28. 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 Microsoft Visual Basic 2008: Reloaded, Third Edition

  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 Edition

  30. 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

  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 Edition

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

  33. 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 2008: Reloaded, Third Edition

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

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

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

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

  38. 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 2008: Reloaded, Third Edition

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

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

  41. 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

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

  43. 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

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

  45. 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

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

  47. 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

  48. 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 Microsoft Visual Basic 2008: Reloaded, Third Edition

  49. 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 Microsoft Visual Basic 2008: Reloaded, Third Edition

  50. 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 Microsoft Visual Basic 2008: Reloaded, Third Edition

More Related