for…next loops, checked list boxes, and combo boxes chapter 5

Post on 08-Jan-2018

224 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

For…Next Loops, Checked List Boxes, and Combo Boxes

Chapter 5

Chapter 5 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.

Chapter 5 3

Syntax

For Counter = Start to Endstatementblock

Next Counter

Chapter 5 4

Counter

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

Chapter 5 5

Start

Initial value of the counter variable. Must be numeric.

Chapter 5 6

End

Value the counter variable is tested against just prior to each iteration of the loop.

Must be numeric.

Chapter 5 7

Next Counter

Marks end of loop. Increments counter automatically.

Chapter 5 8

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

Chapter 5 9

Create this Example (Tutorial 5-7)

Initializecounterto 1

Comparecounterto 10

Increment counter by 1;Go back to compare counter

Body ofloop

Chapter 5 10

Iterations

intCount intSquare Iterations

0 0 Pre loop

1 2 1 ^ 2

2 4 2 ^ 2

3 9 3 ^ 2

4 16 4 ^ 2

10 100 10 ^ 2

Chapter 5 11

For…Next Process

Step 1: Initialize intCount to 1. Step 2: Compare intCount to 10. If intCount is not > 10go to Step 3. Otherwise, terminate the loop.

Step 3: Execute body of loop.

Step 4: Increment intCount by 1. Go back to Step 2.

Chapter 5 12

For…Next Results

Chapter 5 13

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.

Chapter 5 14

Step Option

For counter = start To end Step incrementstatementblock

Next counter

Increment: amount to step countermust be numeric

Default increment = 1 if not stated

Chapter 5 15

Step Example (p. 295)

Increases x by 10

Chapter 5 16

Negative Step Amounts

Counts backwards by subtracting from counter.

If counter >= ending value, execute statementblock.

If counter < ending value, exit the loop.

Chapter 5 17

Negative Step Example

Start counter highEnd counter low

Decrease counter

Chapter 5 18

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)

Chapter 5 19

Summing Numbers

See 1st example on p. 296. Use accumulator to add value of counter

variable. Output final accumulated value after the loop

is completed. See 2nd example on p. 296.

Use input box so that user can specify how many numbers to sum.

Use CInt to convert string to integer.

Chapter 5 20

Exit Loops

Exit For Exits a For…Next loop. See example on p. 297.

Exit Do Exits a Do…Loop.

Chapter 5 21

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.

Chapter 5 22

Nested Loop

Loop inside another loop. Example:

Clock with second hand, minute hand, and hour hand

Indentation is important for readability!

Chapter 5 23

Nested Loop Example (pp. 298-299)

Chapter 5 24

Clock Example

Chapter 5 25

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

Chapter 5 26

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)

Chapter 5 27

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

Chapter 5 28

Copy Checked Items to List Box

Chapter 5 29

CheckOnClick Property

False (default) Click to select Click to check

True Click to select & check

Chapter 5 30

Combo Boxes

Read on your own (pp. 302-304). Complete Tutorial 5-9 (pp. 304-306).

Chapter 5 31

More Input Validation

Review Garbage in Garbage Out Validates user input to ensure appropriate.

Positive value Number not letter Within a range

Chapter 5 32

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

Chapter 5 33

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.

Chapter 5 34

With…End With (p. 314)

Avoid using an object name several times. With txtNum1

.SelectionStart = 0

.SelectionLength = .Text.LengthEnd With

Chapter 5 35

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.

Chapter 5 36

ToolTip Properties

InitialDelay Milliseconds that elapse between mouse over

and ToolTip appearing 1/1000th of second = 1 millisecond ½ of a second = 500 milliseconds

AutoPopDelay Milliseconds that ToopTip remains onscreeen

Chapter 5 37

KeyPress Event

top related