Chris Worfolk's Blog


Using timers in Visual Basic

December 30th, 2004 | Programming, Tech

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

December 30th, 2004 | Programming, Tech

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

December 30th, 2004 | Programming, Tech

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

December 30th, 2004 | Programming, Tech

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

Turning a form button into a link using JavaScript

December 30th, 2004 | Programming, Tech

Bored of ordinary links? Then try using buttons. This can work well if there are a series of them lined up in a navigation bar especially if you using image backgrounds or CSS styles to make them look good.

First of all, there is the script that goes into the head of your page.

<script language="JavaScript">
<!--
function goToURL() { window.location = "http://www.hardwaretutorials.com/"; }
-->
</script>

Amazingly simple. Your normal JavaScript tags with a link function. Now for the button which goes in your pages body tag:

<input type=button value="New JavaScripts" onClick="goToURL()" />

Of course, you should never use buttons, or JavaScript links, because they create usability problems. Also, this is also invalid HTML. But it will work in almost all browsers.

Suppressing JavaScript errors

December 30th, 2004 | Programming, Tech

What is the most annoying thing you cant think of? Well ok, JavaScript error aren’t that annoying but nobody likes them. What we need is a simple script that stops all the JavaScript erros from appearing. This is a pretty simple script which is short so lets dive straight into the code.

<script>
function stoperror(){
return true
}
window.onerror=stoperror
</script>

Just stick the script into the head tag. Although the above alert method is invalid (no closing quotations), no errors will be generated. This script will suppress all potential errors in a page, so be sure to turn it off while test running a script.

Of course, a much better solution, is to actually write JavaScript that works.

A tale of two web servers

December 30th, 2004 | Life

mworld.us is back baby! After the Audio Senate revivial on December 6th (turns out Nerd Federations track back system isn’t what I thought it was 😉 I didn’t think a revival could get much sweeter. But now M World is back it is a great feeling. Even if it did take so many hours.

To begin the story, I renewed by web hosting back at the end of October. My web host, onedollarhost.net, didn’t seem to notice though and deleted everything. Files, databases, apparently they don’t even keep backups. Hmm, so as you can image I wasn’t happy. M World has like a hundred sub-sites so I can’t just upload them all again.

Anyway I emailed them several times asking if they had backups and if so use them to restore the files from that. The response I got over and over was:

“Files were deleted. You will need to re-upload the files.”

Another thing I wanted from that host was stats as I had none. It is a shame I didn’t as it was quite a high traffic site with over 6,000 pages in Google so I would be interested to see what the traffic was like. That is no longer a problem though, as I took the decision to move over to my new Windows host.

I have been with them ages although I didn’t want to move M World because of the data loss (and there has been a lot of data loss). Still it didn’t seem to matter now, so I moved it over and so from now on I will be able to track everything from Awstats. There is some good in every situation I guess.

I have also spend hours last night and most of today putting together all of the sub-sites. I found all but one although there may be a load not listed. Still, I have put all the others online and with no content, hopefully M World will eventually regain it’s former glory.

So although it has not been a good experiance – two months of missing files, most of it is back online now. There are still a few bits I keep remembering but overall, I think M World can now move forward, safe on a new server.

Find your IP address in Windows

December 30th, 2004 | Life, Tech

To find out your ip address:

Select start and click run.

Type “command” and click ok.

Once the command promt appears, type ipconfig and press return. This will then bring up a list of different ip address’s. You will usually be looking for the port you connect to the internet through.

Removing fonts to boot Windows quicker

December 30th, 2004 | Life, Tech

When you first boot Windows up, all the fonts must be loaded. If you delete or move unwanted fonts, Windows will load up quicker.

Prevent applications launching automatically on Windows boot up

December 30th, 2004 | Life, Tech

If you don’t want all your programmes to automatically boot up when Windows starts, hold control key down to skip loading them.