1 / 38

Microsoft Visual Basic: Reloaded

Microsoft Visual Basic: Reloaded. Chapter Seven More on the Repetition Structure. Introducing the Interest Calculator Application For...Next repetition statements Select the existing text in a text box Code the TextChanged event procedure

eiler
Download Presentation

Microsoft Visual Basic: Reloaded

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: Reloaded Chapter Seven More on the Repetition Structure

  2. Introducing the Interest Calculator Application For...Next repetition statements Select the existing text in a text box Code the TextChanged event procedure Code a list box’s SelectedValueChanged and SelectedIndexChanged event procedures Code the TextChanged event procedure for a combo box Store images in an image list control Overview

  3. Interest Calculator Application • You are investing $1,000.00 in a savings account that yields 5% interest compounded annually. Assuming that you leave all interest on deposit, calculate and display the amount of money in the account at the end of each year over a period of n years. To compute these amounts, use the following formula: • a = p (1 + r)n • where • p is the original amount of money invested (the principal) r is the annual interest rate (for example, .05 is equivalent to 5%) n is the number of years a is the amount on deposit at the end of the nth year.

  4. InterestCalculator Application Click to increase number of years NumericUpDown control Click to decrease number of years Multiline TextBox displays application results Figure 11.2|Output of completedInterest Calculator application.

  5. Designing the Interest Calculator Application When the user clicks the Calculate Button Get the values for the principal, interest rate and years entered by the user Store a header String to be added to the output TextBox For each year (starting at 1 and ending with the number of years entered) Calculate the current value of the investment Append the year and the current value of the investment to the String that will be displayed in the output TextBox Display the final output in the output TextBox

  6. For...Next Repetition Statement • The For...Next repetition statement makes it easier for you to write code to perform counter-controlled repetition. • It takes less time to code and is easier to read. • The For...Nextheader (Fig. 11.4) specifies all four essential elements for counter-controlled repetition. Figure 11.4|For...Next header components.

  7. For...Next Repetition Statement For counter AsInteger = 2 To 10 Step 2body statement(s)Next • A For...Next statement begins with the keyword For. • The statement declares and initializes a control variable. • Note that you do not use the Dim keyword. • Following the initial value of the control variable is the keyword To, followed by the final value of the control variable.

  8. Examples Using the For...Next Statement • The following examples demonstrate different ways of varying the control variable in a For...Nextstatement. • Vary the control variable from 1 to 100 in increments of 1. For i As Integer = 1To 100 or For i As Integer = 1To100Step1 • Vary the control variable from 100 to 1 in increments of -1(decrements of 1). For i As Integer = 100To1Step-1

  9. Examples Using the For...Next Statement • Vary the control variable from 7 to 77 in increments of 7. For i As Integer = 7To77Step7 • Vary the control variable from20 to -20 in increments of-2(decrements of 2). For i As Integer = 20To-20Step-2 • Vary the control variable over the sequence of the following values: 2, 5, 8, 11, 14, 17, 20. For i As Integer = 2To20Step3 • Vary the control variable over the sequence of the following values: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0. For i As Integer = 99To0Step-11

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

  11. The For...Next Statement (cont'd.) Figure 7-7: Comparison of the For…Next and Do…Loop statements

  12. NumericUpDown Controls NumericUpDown control Figure 11.9|NumericUpDown control added to InterestCalculator application.

  13. NumericUpDown Controls • By default, this control sets 0 as the minimum and 100 as the maximum. • To change these values, set the Maximum property of the Years: NumericUpDown control to 10. • Then set its Minimum property to 1. • If the user inputs a value less than Minimum or greater than Maximum, the value is automatically set to the minimum or maximum value. • The Increment property specifies by how much thecurrent number in the NumericUpDown controlchanges when the user clicks the control’s up or downarrow. • This application uses the default value, 1.

  14. Adding and Customizing a MultilineTextBox with a Scrollbar • Add a TextBox named resultTextBox (Fig. 11.10). • Change the TextBox’s Multiline property value to True. • Set the ReadOnly property to True. Vertical scrollbar (disabled) Multiline TextBox Figure 11.10|Multiline TextBox with vertical scrollbar added to the Form.

  15. GUI Design Tips • If a TextBox will display multiple lines of output, set the Multiline property to True and left align the output by setting the TextAlign property to Left. • If a multiline TextBox will display many lines of output, limit the TextBox height and use a vertical scrollbar to allow users to view additional lines of output. • If a TextBox is used to display output, set the ReadOnly property to True to ensure that the user cannot change the output.

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

  17. Selecting the Existing Text in a Text Box (cont’d.) Figure 7-17: Loan text box’s Enter event procedure

  18. Selecting the Existing Text in a Text Box (cont’d.) Figure 7-18: Result of processing the text box’s Enter event procedure

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

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

  21. Coding the TextChangedEvent Procedure Figure 7-21: Result of processing the text box’s TextChanged event procedure

  22. Coding the SelectedValueChanged and SelectedIndexChanged Event Procedures Figure 7-22: The list box’s SelectedValueChanged and SelectedIndexChangedevent procedures

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

  24. Using a Combo Box in an Interface Figure 7-23: Examples of the three styles of combo boxes

  25. Figure 7-24: Code used to fill the combo boxes in Figure 7-23 with values

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

  27. Using a Combo Box in an Interface (cont'd.) Figure 7-25: Sample run of the Payment Calculator application using a combo box

  28. Figure 7-26: Code for the Payment Calculator application shown in Figure 7-25

  29. Figure 7-26: Code for the Payment Calculator application shown in Figure 7-25 (cont’d.)

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

  31. Using an Image List Control (cont’d.) Figure 7-27: Image Viewer application

  32. Using an Image List Control (cont’d.) Figure 7-29: Completed Images Collection Editor window in the Image Viewer application

  33. Using an Image List Control (cont’d.) Figure 7-30: How to refer to an image in the Images collection

  34. Using an Image List Control (cont’d.) Figure 7-31: Sample run of the Image Viewer application

  35. Using an Image List Control (cont’d.) Figure 7-32: Code entered in the View button’s Click event procedure

More Related