Using timers in Visual Basic

Timers are those little button things that appear on the wizard for the web browser from in Visual Basic 6. They control time. The first thing to note about them is that they are invisible at run time so you don’t have to worry about where you put them because the user won’t see a thing.

Timers, wait for a specific length of time and then execute the script they have. One of the first options they have is whether to start enabled or disabled. If it’s enabled it will begin timing and execute the script. If it is disabled then it won’t do anything.

Being able to disable it allows you to set it so it only does something once. For instance if you wanted it to wait 10 seconds and then say “hello,” but only do this once you could use this:

Create a timer object called “timer1.” This should happen automatically as timer1 is the default name. Set your time to enabled and set the time period to 10000. The timer is set to one thousandth of a second so 10,000 will make it time for ten seconds.

Next double click on the timer object to bring up your code box. And add in the following:

MsgBox ("Hello")
Timer1.Enable = False

When the timer reaches its time limit it runs the script. The first line just tells it to say “hello” in a message box. The second line disables itself by telling the object “timer1” to set its property, enable to false so that it is disabled.

A timer is also handy in things such as web browsers. Every tenth of a second you could set the timer to check whether a page has loaded and if so change the title of the form to the web browser’s page title.

If WebBrowser1.PageTitle <> Form1.Caption Then
Form1.Caption = WebBrowser1.PageTitle
End If

In this script you want to keep the timer permanently enabled so it will be constantly checking to see if the web browser’s page title and the text in the title bar of the forms are different and if so, update the forms title.

Timeline

Newsletter

Don't have time to check my blog? Get a weekly email with all the new posts. This is my personal blog, so obviously it is 100% spam free.

Metadata

Tags: , ,

This entry was posted on Thursday, December 30th, 2004 at 4:07 pm and is filed under Programming, Tech. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.