1 / 55

Microsoft Visual Basic 2010: Reloaded Fourth Edition

Microsoft Visual Basic 2010: Reloaded Fourth Edition. Chapter Seven More on the Repetition Structure. Objectives. After studying this chapter, you should be able to: Code a counter-controlled loop using the For…Next statement Nest repetition structures

urit
Download Presentation

Microsoft Visual Basic 2010: Reloaded Fourth 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 2010: ReloadedFourth Edition Chapter Seven More on the Repetition Structure

  2. Objectives After studying this chapter, you should be able to: • Code a counter-controlled loop using the For…Next statement • Nest repetition structures • Calculate a periodic payment using the Financial.Pmt method • Select the existing text in a text box • Code the TextChanged event procedure for a text box Microsoft Visual Basic 2010: Reloaded, Fourth Edition 2

  3. Objectives (cont'd.) • Code a list box’s SelectedValueChanged and SelectedIndexChanged event procedures • Include a combo box in an interface • Code the TextChanged event procedure for a combo box • Store images in an image list control • Display an image stored in an image list control Microsoft Visual Basic 2010: Reloaded, Fourth Edition 3

  4. The For...Next Statement • For...Next statement:processes a set of instructions a known number of times • Is a pretest loop • Counter-controlled loop: a loop whose processing is controlled by a counter • Counter variable: used to keep track of the number of times the loop has been processed • Startvalue, endvalue, and stepvalue items • Control the number of times the loop is processed • Must evaluate to numeric values • Can be positive or negative Microsoft Visual Basic 2010: Reloaded, Fourth Edition 4

  5. The For...Next Statement (cont'd.) • A negative stepvalue causes the loop counter to count down • Decrementing: adding increments of a negative number • Flowchart symbol for the For...Next loop is a hexagon • Values for the counter variable, startvalue, stepvalue, and endvalue are shown within the hexagon Microsoft Visual Basic 2010: Reloaded, Fourth Edition 5

  6. Figure 7-1: How to use the For…Next statement Microsoft Visual Basic 2010: Reloaded, Fourth Edition 6

  7. Figure 7-1: How to use the For…Next statement (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 7

  8. The For...Next Statement (cont'd.) Figure 7-2: Processing steps for Example 1 in Figure 7-1 Microsoft Visual Basic 2010: Reloaded, Fourth Edition 8

  9. The For...Next Statement (cont'd.) Figure 7-2: Processing steps for Example 1 in Figure 7-1 (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 9

  10. The For...Next Statement (cont'd.) Figure 7-3: Problem specification for the Savings Account application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 10

  11. The For...Next Statement (cont'd.) Figure 7-4: Sample run of the Savings Account application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 11

  12. Figure 7-5: Pseudocode and flowchart for the Calculate button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 12

  13. The For...Next Statement (cont'd.) Figure 7-6: Calculate button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 13

  14. The For...Next Statement (cont'd.) • Can use For…Next or Do…Loop for a counter-controlled loop • With Do…Loop, you must: • Declare and initialize the counter variable • Update the counter variable • Include an appropriate comparison in the Do clause • For…Next statement handles the declaration, initialization, update, and comparison tasks • Is more convenient to use Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  15. The For...Next Statement (cont'd.) Figure 7-7: Comparison of the For…Next and Do…Loop statements Microsoft Visual Basic 2010: Reloaded, Fourth Edition 15

  16. Nested Repetition Structures • Repetition structures can be nested • Can use pretest or posttest loops for outer loop and for inner (nested) loop • A clock with minute and second hands demonstrates nested loops • Minute hand moves 1 position, then the second hand moves 60 positions Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  17. Figure 7-8: Three versions of the logic used by a clock’s minute and second hands Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  18. Nested Repetition Structures (cont’d.) Figure 7-9: Modified problem specification for the Savings Account application Figure 7-9: Sample run of the Savings Account application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  19. Nested Repetition Structures (cont’d.) Figure 7-11: Two versions of the Calculate button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  20. Figure 7-11: Two versions of the Calculate button’s Click event procedure (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  21. The Payment Calculator Application Figure 7-12: Problem specification for the Payment Calculator application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  22. The Payment Calculator Application (cont’d.) Figure 7-13: Sample run of the Payment Calculator application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  23. 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.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 23

  24. Figure 7-14: How to use the Financial.Pmt method Microsoft Visual Basic 2010: Reloaded, Fourth Edition 24

  25. The Financial.Pmt Method (cont’d.) Figure 7-15: Pseudocode and code for the Calculate button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  26. Figure 7-15: Pseudocode and code for the Calculate button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  27. Selecting the Existing Text in a Text Box • SelectAll method: selects all text in a text box for replacement typing • User only needs to type the new value • Enter event: occurs when the text box receives the focus • When the user tabs to the control • When the user uses the control’s access key • When the Focus method sends the focus to the control • When the user clicks in the text box Microsoft Visual Basic 2010: Reloaded, Fourth Edition 27

  28. Selecting the Existing Text in a Text Box (cont'd.) Figure 7-16: How to use the SelectAll method Microsoft Visual Basic 2010: Reloaded, Fourth Edition 28

  29. Selecting the Existing Text in a Text Box (cont'd.) Figure 7-17: Loan text box’s Enter event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 29

  30. Selecting the Existing Text in a Text Box (cont'd.) Figure 7-18: Result of processing the text box’s Enter event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 30

  31. Selecting the Existing Text in a Text Box (cont'd.) • Entering a new value in the Loan text box does not update the monthly payments until the user clicks the Calculate button • May cause confusion Figure 7-19: New loan amount entered in the Loan text box Microsoft Visual Basic 2010: Reloaded, Fourth Edition 31

  32. Coding the 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 Figure 7-20: New principal entered in the Principal text box Microsoft Visual Basic 2010: Reloaded, Fourth Edition 32

  33. Coding the TextChanged Event Procedure (cont’d.) Figure 7-21: Result of processing the text box’s TextChanged event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 33

  34. Coding the SelectedValueChanged and SelectedIndexChanged Event Procedures Figure 7-22: The list box’s SelectedValueChanged and SelectedIndexChangedevent procedures Microsoft Visual Basic 2010: Reloaded, Fourth Edition 34

  35. Including a Combo Box in an Interface • Combo Box control: • Similar to a list box with a list of choices • List portion may be hidden • May contain a text field that allows the user to type an entry that is not on the list • Three styles of combo boxes, controlled by the DropDownStyle property: • Simple • DropDown (the default) • DropDownList Microsoft Visual Basic 2010: Reloaded, Fourth Edition 35

  36. Using a Combo Box in an Interface Figure 7-23: Examples of the three styles of combo boxes Microsoft Visual Basic 2010: Reloaded, Fourth Edition 36

  37. Figure 7-24: Code used to fill the combo boxes in Figure 7-23 with values Microsoft Visual Basic 2010: Reloaded, Fourth Edition 37

  38. Using a Combo Box in an Interface (cont'd.) • Items.Add method: used to add items to a combo box • 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) • Items.count property: used to obtain the number of items in the combo box • Sorted property: used to sort the items in the list Microsoft Visual Basic 2010: Reloaded, Fourth Edition 38

  39. Using a Combo Box in an Interface (cont'd.) Figure 7-25: Sample run of the Payment Calculator application using a combo box Microsoft Visual Basic 2010: Reloaded, Fourth Edition 39

  40. Figure 7-26: Code for the Payment Calculator application shown in Figure 7-25 Microsoft Visual Basic 2010: Reloaded, Fourth Edition 40

  41. Figure 7-26: Code for the Payment Calculator application shown in Figure 7-25 (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 41

  42. Using an Image List Control • Image List control: • Stores a collection of images • Does not appear on the form; appears in the component tray • Add images to the control using the Images Collection Editor window Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  43. Using an Image List Control (cont’d.) Figure 7-27: Image Viewer application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  44. Using an Image List Control (cont’d.) Figure 7-28: How to add images to an image list control’s Images collection Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  45. Using an Image List Control (cont’d.) Figure 7-29: Completed Images Collection Editor window in the Image Viewer application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  46. Using an Image List Control (cont’d.) Figure 7-30: How to refer to an image in the Images collection Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  47. Using an Image List Control (cont’d.) Figure 7-31: Sample run of the Image Viewer application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  48. Using an Image List Control (cont’d.) Figure 7-32: Code entered in the View button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  49. Programming Tutorial 1 • Creating the Slot Machine Application Figure 7-34: MainForm in the Slot Machine application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 49

  50. Programming Tutorial 2 • Creating the Birmingham Sales Application Figure 7-40: MainForm for the Birmingham Sales application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 50

More Related