Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

Chapter 7 Using Loops and Timers 201


True starts the timer running when the program starts. (If the timer wasn’t needed until
later in the program, you could disable this property and then enable it in an event
procedure .)
Your form looks like this:


  1. Double-click the timer object in the component tray, and then type the following
    statements in the Timer1_Tick event procedure:


MsgBox("Sorry, your time is up.")
End
The first statement displays a message indicating that the time has expired, and the
second statement stops the program. Visual Basic executes this event procedure if the
timer interval reaches 15 seconds and a valid password hasn’t been entered.


  1. Display the form, double-click the button object, and then type the following statements
    in the Button1_Click event procedure:


If TextBox1.Text = "secret" Then
Timer1.Enabled = False
MsgBox("Welcome to the system!")
End
Else
MsgBox("Sorry, friend, I don't know you.")
End If
This program code tests whether the password entered in the text box is “secret .” If it is,
the timer is disabled, a welcome message is displayed, and the program ends. (A more
useful program would continue working rather than ending here .) If the password
entered isn’t a match, the user is notified with a message box and is given another
chance to enter the password. But the user has only 15 seconds to do so!


  1. Click the Save All button on the Standard toolbar to save your changes. Specify the
    C:\Vb10sbs\Chap07 folder as the location.

Free download pdf