For Loops

Speaker Notes

Slide 1:

Notes on using For Loops in Visual Basic.

Slide 2:

This code will show a For Loop doing a calculation and displaying the results in a picture box and in a text box.

Slide 3:

This slide shows the output from the two four loops. Again one goes in a Picture Box and one goes in a Text Box.

Note that the picture box and the text box were not big enough to receive all of the data.

Note that to clear the picture box use the name of the box .Cls for clear screen. With the text box, simply set it to null.

Slide 4:

Note that for this example, I am assuming that ct and answer have been defined with Dim statements.

Slide 5:

When the inner loop is complete and ct1 is incremented by 1, control drops down to the For ct2 as opposed to returning to it from the next ct2. This means that ct2 is initialized at 1.

When control is through the next, ct2 is incremented.

When control drops down from the outer For loop, ct2 is initialized at 1.

Slide 6:

You can create a line break in a text box by inserting a carriage control followed by a line feed. The carriage control is the ANSI 13 and the line feed is ANSI 10. These can be entered as Chr(13) and Chr(10).

Remember concatenation puts things together as a unit. In Visual Basic, concatenation is done with the &. Technically it can also be done with the +, but this is discouraged since it can be confused with addition.

Slide 7:

The scroll bar is a property of the text box so it is set up when the text box is created.

The code is on the next slide.

Slide 8:

In this example, I have organized the properties of the TextBox by using the Categorized tab. In previous examples, I have used the Alphabetic tab. The Categorized organization simply groups the properties by appearance, behavior etc. and can make things easier to find. In this instance, I look under behavior for ScrollBars. Note that they are set to option 2 which is a Vertical scroll bar.

Note that ScrollBar is not a property of a picture box.

Slide 9:

To review, this is a single For loop where X varies from 1 t0 10. When X reaches 10 the For loop is over and since there is no other code in the procedure, cmdCalc is also done.

Slide 10:

This shows another example of a nested FOR loop where X is controlled by the outer loop and Y is controlled by the inner loop. Again, concatenation is not used and the elements in the picFor.Print are separated by semi-colons.

Slide 11:

Note that the picture says ProjFor2 - apparently I forgot to change the names!

Slide 12:

Note that with a negative step, X and Y start with larger values that will be reduced.