Presentation is loading. Please wait.

Presentation is loading. Please wait.

For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.

Similar presentations


Presentation on theme: "For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5."— Presentation transcript:

1 For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5

2 2 For…Next Loop Ideal for situations that require a counter. Specifically designed to initialize, test, and increment a counter variable. Unlike Do…While and Do…Until loops, For…Next has a specific number of iterations.

3 Chapter 53 Syntax For Counter = Start to End statementblock Next Counter

4 Chapter 54 Counter Variable used as a counter. Must be numeric. Variable initialized by programmer. Must appear in For line. Optional, but recommended, in Next line.

5 Chapter 55 Start Initial value of the counter variable. Must be numeric.

6 Chapter 56 End Value the counter variable is tested against just prior to each iteration of the loop. Must be numeric.

7 Chapter 57 Next Counter Marks end of loop. Increments counter automatically.

8 Chapter 58 Squares Example Find the square of values 1 through 10. Display the value and its square as a string: The square of 2 is 4. Use ToString to convert an integer to a string: strOutput.ToString

9 Chapter 59 Create this Example (Tutorial 5-7) Initialize counter to 1 Compare counter to 10 Increment counter by 1; Go back to compare counter Body of loop

10 Chapter 510 Iterations intCountintSquareIterations 00Pre loop 121 ^ 2 242 ^ 2 393 ^ 2 4164 ^ 2 1010010 ^ 2

11 Chapter 511 For…Next Process Step 1: Initialize intCount to 1. Step 2: Compare intCount to 10. If intCount is not > 10 go to Step 3. Otherwise, terminate the loop. Step 3: Execute body of loop. Step 4: Increment intCount by 1. Go back to Step 2.

12 Chapter 512 For…Next Results

13 Chapter 513 For…Next or Do…Loop For…Next # of iterations can be determined when the loop is entered. Do…Loop Only when # of iterations depends on results inside loop.

14 Chapter 514 Step Option For counter = start To end Step increment statementblock Next counter Increment: amount to step counter must be numeric Default increment = 1 if not stated

15 Chapter 515 Step Example (p. 295) Increases x by 10

16 Chapter 516 Negative Step Amounts Counts backwards by subtracting from counter. If counter >= ending value, execute statementblock. If counter < ending value, exit the loop.

17 Chapter 517 Negative Step Example Start counter high End counter low Decrease counter

18 Chapter 518 Particulars Changing the start, end, and step variables inside the For…Next loop do not affect counters since initialization already occurred. Changing the counter variable inside the For…Next loop can cause problems; do not change this variable! (Warning on p. 292)

19 Chapter 519 Summing Numbers See 1 st example on p. 296. Use accumulator to add value of counter variable. Output final accumulated value after the loop is completed. See 2 nd example on p. 296. Use input box so that user can specify how many numbers to sum. Use CInt to convert string to integer.

20 Chapter 520 Exit Loops Exit For Exits a For…Next loop. See example on p. 297. Exit Do Exits a Do…Loop.

21 Chapter 521 Loop Decisions Do While Want loop to repeat as long as test expression is true. Doesn’t loop if false. Write as pretest if you do not want loop to iterate if expression is false from beginning. Write as posttest if want to loop to iterate once. Do Until Want loop to repeat as long as test expression is false. Doesn’t loop if true. Write as pretest if don’t want to loop if test expression is true from beginning. Write as posttest if want loop to iterate at least once. For Next Initializes counter variable to a start value. Increments automatically at the end of the iteration. Loops as long as counter variable is not greater than an end value (or less than end value for decrementing). Use when number of iterations is known upfront.

22 Chapter 522 Nested Loop Loop inside another loop. Example: Clock with second hand, minute hand, and hour hand Indentation is important for readability!

23 Chapter 523 Nested Loop Example (pp. 298-299)

24 Chapter 524 Clock Example

25 Chapter 525 Using For…Next Loops for Lists (Figure 7.32 on p. 364)

26 Chapter 526 Checked List Box Control Variation of a list box Supports all ListBox control properties and methods. Selected property (only 1 at a time) Checked property (multiple can be checked)

27 Chapter 527 Checked List Box (pp. 300-302) Selected: Provo (only) Checked: Layton, Ogden, Park City, Provo

28 Chapter 528 Copy Checked Items to List Box

29 Chapter 529 CheckOnClick Property False (default) Click to select Click to check True Click to select & check

30 Chapter 530 Combo Boxes Read on your own (pp. 302-304). Complete Tutorial 5-9 (pp. 304-306).

31 Chapter 531 More Input Validation Review Garbage in  Garbage Out Validates user input to ensure appropriate. Positive value Number not letter Within a range

32 Chapter 532 Validating Event Triggers just before focus shifts to another control whose CausesValidation property is true. Enables you to write error messages and keep focus to text box instead of shifting focus to another control

33 Chapter 533 Input Validation Complete Tutorial 5-10 now. Add code for Validating event. Perform software testing by experimenting with different number combinations. Include two GotFocus events to select contents upon focus.

34 Chapter 534 With…End With (p. 314) Avoid using an object name several times. With txtNum1.SelectionStart = 0.SelectionLength =.Text.Length End With

35 Chapter 535 ToolTips (aka ScreenTips) Small box that displays when the mouse is over a control. Short description of the control. ToolTip control provides ToolTip property for all controls on a form.

36 Chapter 536 ToolTip Properties InitialDelay Milliseconds that elapse between mouse over and ToolTip appearing 1/1000 th of second = 1 millisecond ½ of a second = 500 milliseconds AutoPopDelay Milliseconds that ToopTip remains onscreeen

37 Chapter 537 KeyPress Event


Download ppt "For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5."

Similar presentations


Ads by Google