Chapter 7 Using Loops and Timers 183
Using a Counter Variable in a Multiline TextBox Control
A counter variable is just like any other variable in an event procedure. It can be assigned
to properties, used in calculations, or displayed in a program. One of the practical uses for
a counter variable is to display output in a TextBox control. You used the TextBox control
earlier in this book to display a single line of output, but in this chapter, you’ll display many
lines of text by using a TextBox control. The trick to displaying more than one line is simply
to set the Multiline property of the TextBox control to True and to set the ScrollBars property
to Vertical. Using these simple settings, the one-line text box object becomes a multiline text
box object with scroll bars for easy access.
Display information by using a For... Next loop
- Start Visual Studio, and create a new Microsoft Visual Basic Windows Forms Application
project named My For Loop.
A blank form opens in the Designer. Your first programming step is to add a Button
control to the form, but this time you’ll do it in a new way. - Double-click the Button control in the Toolbox.
Visual Studio places a button object in the upper-left corner of the form. With
the Button control and many others, double-clicking is a quick way to create a
standard-sized object on the form. Now you can drag the button object where you
want it and customize it with property settings.
- Drag the button object to the right, and center it near the top of the form.
- Open the Properties window, and then set the Text property of the button to “Loop .”
- Double-click the TextBox control in the Toolbox.
Visual Studio creates a small text box object on the form.
- Set the Multiline property of the text box object to True, and then set the ScrollBars
property of the text box object to Vertical.
Note The TextBox1 object contains a smart tag, which you can use to set the Multiline
property to True. Collectively, the Multiline and ScrollBars properties prepare the text box
for displaying more than one line of text.
- Move the text box below the button, and enlarge it so that it takes up two-thirds of
the form. - Double-click the Loop button on the form.
The Button1_Click event procedure appears in the Code Editor.