dcsimg
December 4, 2016
Hot Topics:

Referencing an Object

  • November 19, 2002
  • By Neil Avent
  • Send Email »
  • More Articles »

Someone who looks at this project without understanding the code might be under the impression that you have just loaded Internet Explorer and added a bit of code to hide it.

From a visible point, you could see why they would think that, but the underlying code is far more intelligent. If we started Internet Explorer from a Shell call, and used subclassing to hide the window, we could easily gain the same effect as this example. However, this method of exposing Internet Explorer as an object instead of an application puts us in a far more powerful position when it comes to controlling and understanding what it is doing.

We have 3 command buttons on our form, lets look at each one and what they do.

Under cmdStart we have the following code:

Private Sub cmdStart_Click()
    Set ie = New InternetExplorer
    cmdStart.Enabled = False
    cmdVisible.Enabled = True
    cmdStop.Enabled = True
End Sub

So when you clicked on the Start command button the following happened.

  • IE was set to a new instance of the InternetExplorer class
  • The cmdStart button was disabled so the user could not click on it again.
  • The cmdVisible and cmdStop buttons were enabled so the user could click on them

It sounds quite straightforward and even after a closer look it remains so. The only bit I will discuss here is the Set Statement. With a datatype, once you have declared the variable (with a Dim statement) you have to place a value into the variable before you get any use out of it. Objects run along the same lines, we used the Dim statement to tell VB what form the object would be but you need the New keyword to actually create an instance of an object. So in this scenario the InternetExplorer class object was exposed to us at design time even though we didn't run it until the Start button was clicked.

Under the cmdVisible button we have this code:

Private Sub cmdVisible_Click()
    ie.Visible = Not (ie.Visible)
End Sub

This toggles the visible property of the IE class. I found out that the visible property was available by looking at the value in the Object Browser. By default, any object created is not visible so when using them in your projects it is good practice to make them visible, especially when debugging.

The final command button was cmdStop, which has this code:

Private Sub cmdStop_Click()
    ie.Quit
    Set ie = Nothing
    cmdStart.Enabled = True
    cmdVisible.Enabled = False
End Sub

This runs the quit method contained in the InternetExplorer class, clears the pointer to the class from your project and finally sets the enabled states of your buttons.

We close the class this way to release system resources that can be used by other applications. The Quit method closes the InternetExplorer window and the Set statement clears the pointer from your project. If the quit method was not run the pointer would have been cleared and the Internet Explorer interface would have been left running regardless of whether it was visible or not. This could be used in an instance where you want your program to stop running but leave Internet Explorer going. Once the pointer had been cleared Internet Explorer would be waiting for a response from the user.

Now we have seen what each of the command buttons have done let us take a look at the first statement we added to our code for this project.





Page 4 of 6



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel