186 Part II Programming Fundamentals
specify a different value for start in the loop and then use the Step keyword to increment
the counter at different intervals. For example, the code:
Dim i As Integer
Dim Wrap As String
Wrap = Chr(13) & Chr(10)
For i = 5 To 25 Step 5
TextBox1.Text = TextBox1.Text & "Line " & i & Wrap
Next i
displays the following sequence of line numbers in a text box:
Line 5
Line 10
Line 15
Line 20
Line 25
You can also specify decimal values in a loop if you declare i as a single-precision or
double-precision type. For example, the For... Next loop:
Dim i As Single
Dim Wrap As String
Wrap = Chr(13) & Chr(10)
For i = 1 To 2.5 Step 0.5
TextBox1.Text = TextBox1.Text & "Line " & i & Wrap
Next i
displays the following line numbers in a text box:
Line 1
Line 1.5
Line 2
Line 2.5
In addition to displaying the counter variable, you can use the counter to set properties,
calculate values, or process files. The following exercise shows how you can use the
counter to open Visual Basic icons that are stored on your hard disk in files that have
numbers in their names. You’ll find many icons, bitmaps, and animation files in the
C:\Program Files\Microsoft Visual Studio 10 .0\Common7\Vs2010imagelibrary folder.
These files are contained in a compressed .zip file, so you will need to extract the files.
These files are not included in Visual Basic 2010 Express. Also note that Microsoft changes
the location for these types of files on occasion.
Open files by using a For... Next loop
- On the File menu, click the New Project command.
The New Project dialog box opens.