184 Part II Programming Fundamentals
- Type the following program statements in the procedure:
Dim i As Integer
Dim Wrap As String
Wrap = Chr(13) & Chr(10)
For i = 1 To 10
TextBox1.Text = TextBox1.Text & "Line " & i & Wrap
Next i
This event procedure declares two variables, one of type Integer (i) and one of type
String (Wrap). It then assigns a string value representing the carriage return character
to the second variable.
Tip In programmer terms, a carriage return character is the equivalent of pressing the
ENTER key on the keyboard. I created a special variable for this character in the program
code, which is made up of return and linefeed elements, to make coding a carriage return
less cumbersome. The return element, Chr(13) moves the I-beam to the beginning of the
line. The linefeed element, Chr(10), reminiscent of an older style typewriter, moves the
I-beam to the next line.
After the variable declaration and assignment, I use a For... Next loop to display
Line X 10 times in the text box object, where X is the current value of the counter
variable (in other words, Line 1 through Line 10). The string concatenation
characters (&) join together the component parts of each line in the text box. First, the
entire value of the text box, which is stored in the Text property, is added to the object
so that previous lines aren’t discarded when new ones are added. Next, the Line string,
the current line number, and the carriage return character (Wrap) are combined to
display a new line and move the I-beam to the left margin and down one line. The Next
statement completes the loop.
Note that Visual Studio automatically adds the Next statement to the bottom of the
loop when you type For to begin the loop. In this case, I edited the Next statement to
include the i variable name—this is an optional syntax clarification that I like to use.
(The variable name makes it clear which variable is being updated, especially in nested
For... Next loops .)
- Click the Save All button on the Standard toolbar to save your changes, and specify
the C:\Vb10sbs\Chap07 folder as the location.
Now you’re ready to run the program.
Tip The complete For Loop program is available in the C:\Vb10sbs\Chap07\For Loop
folder.
- Click the Start Debugging button on the Standard toolbar.