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.

Adding data to the registry in Visual Basic

This sample shows a bottom which when clicked changes the registry. The first three bits of information are the categorises in the registry while the final one is the value.

Private Sub button1_Click()
SaveSetting "My App", "Options", "Clicked", "Yes"
End Sub

You can also make the information dynamic. For instance if you wanted to save a users name you could have a text box called Name1. When the clicked a button it would then save the name they entered into the registry.

Private Sub button1_Click()
SaveSetting "My App", "Users", "Username", Name1.Text
End Sub

Reading from the Registry in Visual Basic

In a previous article I showed you how to save settings in the registry. Now I’m going to show you how to get them back out again.

The basic syntax is as follows.

GetSetting("MyApp", "Category", "InfoName", "DefaultValue")

So lets look at the article on saving information in the registry. I used this example to save a username.

SaveSetting "My App", "Users", "Username", Name1.Text

Now, we’re going to pull this information out and and display the username in the title of the application. This code goes in the Load sub in your application. The name of the form we are working on is irrelevant because I used the term “me” which refers to the current form the code is on.

Me.Caption = "Weclcome " + GetSetting("MyApp", "Users", "Username", "To MyApp")

If no value is found in the registery, the default value is used so if no username was present, the applications title would be.

Welcome To MyApp

If the username in the register was “Jimbo” the applications title would be.

Welcome Jimbo

When using GetSettings you can’t use it on its own. You must use as as part of an equasion such as the example below.

A = GetSettings

You can also do the following.

If GetSettings = a Then do b

It simply supplies one piece of information from the Registry just like a variable.

Trapping errors in Visual Basic

If you keep coming up with problems in your applications and you don’t want error messages to be popping up when the user causes an error then all you need to do is add an error handler. In fact, you should be doing this anyway, just in case errors come up.

On Error

There are two options from here. You can either get it to ignore the error or do something else that sorts the error out. If you just want the error to be ignored use this.

On Error Resume Next

This will simply execute the next line of code after one with the error on. This could stop your application doing something important but lets face it, however there are times when you legitimately want to stop an error and ignore it. Usually it is just used then there is a problem such as when you are using the web browser control and a user clicks back when there is no page to go back to. This would normally bring up an error box but by adding in the code it stops this from happening.

Your other alternative is to add in a GOTO command to send the code to do something else if it finds an error. Take a look at this example.

On Error Goto 10
WebBrowser1.GoBack
Exit Sub
10 ' error bit
MsgBox ("Error!")
' some code to sort it out
End Sub