Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1
181

Chapter 7


Using Loops and Timers


After completing this chapter, you will be able to:
n Use a For... Next loop to execute statements a set number of times.
n Display output in a multiline text box by using string concatenation.
n Use a Do loop to execute statements until a specific condition is met.
n Use the Timer control to execute code at specific times.
n Create your own digital clock and timed password utility.
n Use the Insert Snippet command to insert ready-made code templates or snippets
into the Code Editor.
In Chapter 6, “Using Decision Structures,” you learned how to use the If... Then and Select
Case decision structures to choose which statements to execute in a program. You also
learned how to process user input and evaluate different conditions in a program and how
to determine which block of program statements to execute based on changing conditions.
Now you’ll continue learning about program execution and flow control by using loops to
execute a block of statements over and over again. You’ll also create a digital clock and
other interesting utilities that perform actions at set times or in relation to intervals on your
computer’s system clock.

In this chapter, you’ll use a For... Next loop to execute statements a set number of times,
and you’ll use a Do loop to execute statements until a conditional expression is met. You’ll
also learn how to display more than one line of text in a text box object by using the string
concatenation (&) operator, and you’ll learn how to use the Microsoft Visual Studio Timer
control to execute code at specific intervals in your program. Finally, you’ll learn how to use
the Insert Snippet command to insert code templates into your programs—a time-saving
feature within the Visual Studio Integrated Design Environment (IDE).

Writing For Next Loops


With a For... Next loop, you can execute a specific group of program statements a set
number of times in an event procedure or a code module. This approach can be useful if
you’re performing several related calculations, working with elements on the screen, or
processing several pieces of user input. A For... Next loop is really just a shorthand way
of writing out a long list of program statements. Because each group of statements in
such a list does essentially the same thing, you can define just one group of statements
and request that it be executed as many times as you want.

Table of Contents


Using Loops and Timers.................................... 181


Writing For... Next Loops.......................................... 181
Using a Counter Variable in a Multiline
TextBox Control............................................. 183
Creating Complex For... Next Loops................................. 185
Using a Counter That Has Greater Scope......................... 189
Writing Do Loops................................................... 192
Avoiding an Endless Loop............................................ 193
The Timer Control.................................................. 196
Creating a Digital Clock by Using a Timer Control...................... 197
Using a Timer Object to Set a Time Limit.............................. 200
One Step Further: Inserting Code Snippets............................. 203
Chapter 7 Quick Reference........................................... 207
Free download pdf