Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

Chapter 7 Using Loops and Timers 185



  1. Click the Loop button.


The For... Next loop displays 10 lines in the text box, as shown here:


  1. Click the Loop button again.


The For... Next loop displays another 10 lines on the form. (You can see any
nonvisible lines by using the vertical scroll bar to scroll down .) Each time the loop is
repeated, it adds 10 more lines to the text box object.

Tip Worried about running out of room in the text box object? It will take a while if you’re
displaying only simple text lines. The maximum number of characters is specified in the
MaxLength property for a text box. By default, MaxLength is set to 32,767 characters.
If you need more characters, you can increase this value. If you want more formatting
options, you can use the RichTextBox control in the Toolbox—a similar but even more
capable control for displaying and manipulating text.


  1. Click the Close button on the form to stop the program.


As you can see, a For... Next loop can considerably simplify your code and reduce the
total number of statements that you need to type. In the previous example, a loop
three lines long processed the equivalent of 10 program statements each time you
clicked the Loop button.

Creating Complex For Next Loops


The counter variable in a For... Next loop can be a powerful tool in your programs. With
a little imagination, you can use it to create several useful sequences of numbers in your
loops. To create a loop with a counter pattern other than 1, 2, 3, 4, and so on, you can
Free download pdf