1 / 73

Microsoft Visual Basic 2005: Reloaded Second Edition

Microsoft Visual Basic 2005: Reloaded Second Edition. Chapter 5 Repeating Program Instructions. Objectives. After studying this chapter, you should be able to: Include the repetition structure in pseudocode and in a flowchart Write a For...Next statement

dbeckham
Download Presentation

Microsoft Visual Basic 2005: Reloaded Second 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 2005: Reloaded Second Edition Chapter 5 Repeating Program Instructions

  2. Objectives After studying this chapter, you should be able to: • Include the repetition structure in pseudocode and in a flowchart • Write a For...Next statement • Calculate a periodic payment using the Financial.Pmt method • Include a list box and a combo box in an interface • Write a Do...Loop statement Microsoft Visual Basic 2005: Reloaded, Second Edition

  3. Objectives (continued) • Initialize and update counters and accumulators • Display a dialog box using the InputBox function • Create a multiline text box that cannot be edited • Animate a control by moving it across a form • Have the computer sound a beep Microsoft Visual Basic 2005: Reloaded, Second 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 0 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 2005: Reloaded, Second Edition

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

  6. The For...Next Statement • For...Next statement • Processes a set of instructions a known number of times • Is a pretest loop Microsoft Visual Basic 2005: Reloaded, Second Edition

  7. The For...Next Statement (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  8. The For...Next Statement (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  9. The For...Next Statement (continued) • Startvalue, endvalue, and stepvalue items • Control the number of times the loop is processed • Must evaluate to numeric values • Can be positive or negative • A negative stepvalue causes the loop counter to count down • Flowchart symbol for the For...Next loop is a hexagon Microsoft Visual Basic 2005: Reloaded, Second Edition

  10. The For...Next Statement (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  11. The For...Next Statement (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  12. The For...Next Statement (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  13. The For...Next Statement (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  14. The Financial.Pmt Method • Financial.Pmt method • Calculates a periodic payment on a loan or investment • Returns the periodic payment as a Double type value • Rate and number of periods arguments must be expressed in the same units (monthly, annual, etc.) • Also calculates the amount that must be saved each period to accumulate a specific sum Microsoft Visual Basic 2005: Reloaded, Second Edition

  15. The Financial.Pmt Method (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  16. The Financial.Pmt Method (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  17. The Monthly Payment Calculator Application Microsoft Visual Basic 2005: Reloaded, Second Edition

  18. The Monthly Payment Calculator Application (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  19. Selecting the Existing Text in a Text Box • Windows standard: highlight the existing text when a text box receives the focus • SelectAll method: selects all text in a text box • Enter event: occurs when the text box receives the focus Microsoft Visual Basic 2005: Reloaded, Second Edition

  20. Selecting the Existing Text in a Text Box (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  21. Selecting the Existing Text in a Text Box (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  22. Selecting the Existing Text in a Text Box (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  23. Coding a Control’s TextChanged Event Procedure • TextChanged event • Occurs when a change is made in a control’s Text property • Change may be made by user or the program Microsoft Visual Basic 2005: Reloaded, Second Edition

  24. Coding a Control’s TextChanged Event Procedure (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  25. Coding a Control’s TextChanged Event Procedure (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  26. Using a List Box in an Interface • ListBox tool: creates a ListBox control • ListBox control: displays a list of choices from which the user can select 0 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 2005: Reloaded, Second Edition

  27. Using a List Box in an Interface (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  28. 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 • Index: • A unique number that identifies an item in a collection • Is zero-relative: the first item has index of 0 • Add method: adds an item to the list box’s Items collection Microsoft Visual Basic 2005: Reloaded, Second Edition

  29. Adding Items to a List Box (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  30. Adding Items to a List Box (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  31. Adding Items to a List Box (continued) • Sorted property: • Determines if the list box items are sorted • Sort order is dictionary order Microsoft Visual Basic 2005: Reloaded, Second Edition

  32. Adding Items to a List Box (continued) Microsoft Visual Basic 2005: Reloaded, Second 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 2005: Reloaded, Second Edition

  34. The SelectedItem and SelectedIndex Properties (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  35. The SelectedItem and SelectedIndex Properties (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  36. The SelectedValueChanged and SelectedIndexChanged Events • SelectedValueChanged and SelectedIndexChanged events: occur when a user selects an item in a list box Microsoft Visual Basic 2005: Reloaded, Second Edition

  37. The SelectedValueChanged and SelectedIndexChanged Events (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  38. Modifying the Monthly Payment Calculator Application Microsoft Visual Basic 2005: Reloaded, Second Edition

  39. Modifying the Monthly Payment Calculator Application (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  40. Modifying the Monthly Payment Calculator Application (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  41. Using a Combo Box in an Interface • ComboBox tool: creates a combo box control • ComboBox control: • Similar to a list box • May contain a text field that allows the user to type an entry that is not on the list • List portion may be hidden • Three styles of combo boxes: • Simple • DropDown • DropDownList Microsoft Visual Basic 2005: Reloaded, Second Edition

  42. Using a Combo Box in an Interface (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  43. Using a Combo Box in an Interface (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  44. Using a Combo Box in an Interface (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  45. Using a Combo Box in an Interface (continued) • SelectedItem property: contains the value of the selected item in the list • Text property: contains the value that appears in the text portion of the control (item selected or typed in) Microsoft Visual Basic 2005: Reloaded, Second Edition

  46. Using a Combo Box in an Interface (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  47. Using a Combo Box in an Interface (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  48. Using a Combo Box in an Interface (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  49. Using a Combo Box in an Interface (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  50. The Do...Loop Statement • Do...Loop statement: codes both a pretest or a posttest loop • Use While or Until to code the condition for the loop • Repetition symbol in a flowchart is the diamond Microsoft Visual Basic 2005: Reloaded, Second Edition

More Related