As I explained earlier in this tutorial, If Then statements usually test variables. On this page, I will show you how you may do so. We will make a command button multi-functional without using checkboxes or radio buttons. I will also introduce the timer control to you.
Now for the timer control. The timer simply executes statements in intervals according to the interval property.
Open new form or add a new form to a project.
Name the form frmTimer.
Size the form's width property to 6420 and its height property to 5070.
Set the form's caption to Timer
Change the BorderStyle to FixedSingle and the MaxButton property to False.
Double-click the command button tool to add one to the form; name it cmdClick.
Set its caption to Click Me.
Center it on the bottom of the screen.
Add a label to the form and name it lblRoll.
Set the left property for this label to -2500. You should see it leave the screen. That is fine. We want it to do so.
The Left property simply determines an objects position to the left and right of the form as you noticed.
Double-click the Timer control to add it to the form. Rename it Timer. Set its Enabled property to False.
Set its Interval property to 100.
Double-click the Timer control to add another timer to the form. Set its Enabled property to False.
Set the Enabled property for the timer you just added to False and the Interval to 100.
Lock the controls
Recall: Format > Lock Controls.
OR
Right-click on the form and select Lock Controls.
(Probably getting tired of seeing this step written out huh? OK!! Now YOU need to remember it from now on!)
Double-click the command button on the screen to open the code window and type the following line:
Timer.Enabled = True 'Enables the timer
Click on the object box in the code window and select the Timer object
Type the following line of code:
lblRoll.Left = lblRoll.Left + 120 'Adds 120 the lblRoll's left property according to the set interval
If you run the program, you see the label cross the form from left to right after you click the command button.
Now lets declare a variable.
Select the General option in the code window's object box.
Type Dim Clicks As Byte
This creates a form-level variable that stores values up to 255. We will not go above 2.
In the cmdClick sub-procedure, enter the following line above the line we created earlier:
Clicks = Clicks + 1 'Simply adds 1 to the variable Clicks
every time the object is clicked.
In the cmdClick sub-procedure, enter the following lines below the line we just created:
If Clicks = 2 Then
Timer.Enabled = False 'Disable Timer if Clicks is 2
Timer1.Enabled = True 'Enable Timer1 if Clicks is 2
cmdClick.Enabled = False 'Disable cmdClick if Clicks is 2
Clicks = 0 'Sets clicks back to zero once all the conditions are executed.
Otherwise, clicks will continue to increment by 1 and no condition will
support it
End If
In the Timer1 sub-procedure, enter the following lines
If lblRoll.Top = -600 Then
Timer1.Enabled = False 'Stops timer if lblRoll.Top is -600
lblRoll.Left = -2500 'Sets the label back to initial positions under same
condition
lblRoll.Top = 600 'Sets the label back to initial positions under same
condition
End If 'Ends If statement
Now I will show you how to do a very basic procedure using the timer control again. Specifically, displaying the time with the Time$ function.
Add a new form to you current project: Project Menu > Add Form
Name the form frmDateTime
Set the BorderStyle property to 1-Fixed Single and the MinButton property to False
Select the Caption property and make it blank
Add a timer to the form and set its interval to 1000
Name the timer Timer
Double-click the label tool to add it to the form
Position it in the left corner of the form and set its AutoSize property to True
Name the label lblDate
Now we will code the application
Double-click the timer and type the following code
frmDateTime.Caption = Time$
The line above simply tells the computer to display the time in the form's caption. If you run the application, you will notice that the time updates and that it is in a 24-hour format, as opposed to 12-hour format (AMPM). To change this, simply use the format statement we used earlier as seen below:
frmDateTime.Caption = Format(Time$, "h:mm:ss AMPM")
The line above should be written in the Timer control as done before. This line sets the time format to 12-hour format as you might have guessed.
Now we can display the date using the Date$ function. In the form load event in the code window (double-click form), type the following code:
lblDate.Caption = Date$
The line above puts today's date the the label lblDate. Notice how it displays as 00-00-0000 format. To change this, we can use the format command again to change it as we please. The following line displays the full date:
lblDate.Caption = Format(Date$, "dddd, mmmm d, yyyy")
The quotes take the spaces and commas as literals, so make sure that you typed it (or pasted it) EXACTLY as shown above.
There are TONS of different ways I could have introduced the Timer control. Try to experiment with it a bit to teach yourself more.
I hope this tutorial made sense to you. Hope you enjoyed it as well. Now it is time to proceed to Page 12.