Web Programming in Visual Basic .NET

Web Browsing functionality is available in the .Net Framework Class Library. In this article we will look at techniques for loading and manipulating web pages using Visual Basic .Net. You will need Microsoft Visual Basic .Net 2005 to use the example code.

The Web Browser Object


Let's look at the Web Browser Object. This object is present in the .NET Framework Class Library. It is new to the framework in version 2.0. If you distribute a program that uses the object, the target system must have version 2.0 (or greater) installed.

Public wbInCode As New WebBrowser

The WebBrowser object does not require a form. You can use it to load web pages programmatically without displaying them. However, your program is subject to the configuration settings that you have set for your traditional browser. For example, if you are allowing popups, then any web page that you load into the wbInCode object will have permission to launch popups. The popups will launch in browser windows that are external to your program, even if your WebBrowser object is not displaying in a window.

If a JavaScript method crashes on a loaded page and your are configured to halt on such errors, the web browser control will also halt. Your program will hang. In short, if you plan to use the control in an automated environment you will need to take a close look at your default browser settings. Otherwise you will find that your application has been held up by defective web pages and your display has been inundated with popups.

Load a WebPage into the Browser Object


Use the Navigate method of the WebBrowser object to load a web page:


Wait until the load operation is finished:

 While (wbInCode.IsBusy = true)
   ' Theoretically we shouldn’t need this, but experience says otherwise.
   ' If we just bang on the IsBusy() method we will use up a lot of CPU time
   '  and probably bog down the entire computer.

You can also have the object tell you when it's finished. The DocumentCompleted event fires after a page has loaded. This example adds an event handler method that will be called when the page has finished loading.

 Private Sub MyWebPagePrinter()
   Dim wbInCode As New WebBrowser   ' Create a WebBrowser instance. 
   ' Add an event handler. The AddHandler command ‘connects’ your method called
   ' ReadyToPrint() with the DocumentCompleted event. The DocumentCompleted event
   ' fires when your WebBrowser object finishes loading the web page.
   AddHandler wbInCode.DocumentCompleted, _ 
              New WebBrowserDocumentCompletedEventHandler (AddressOf ReadyToPrint)
   ' Set the Url property to load the document.
 End Sub

We’re not done yet. Here is the ReadyToPrint method that we referred to in the previous code. The name of the method is arbitrary but the argument list is necessary. Note that you don't have to explicitly call this method. It will be called for you by the WebBrowser object after the web page has loaded.

   Private Sub ReadyToPrint(ByVal sender As Object, _
                             ByVal e As WebBrowserDocumentCompletedEventArgs)
       ' Create a temporary copy of the web browser object.
       '  At the same time we will assign it a value by coercing the sender argument
       '  that was passed into this method.
       Dim webBrowserTmp As WebBrowser = CType(sender, WebBrowser)
       ' We know that the web page is fully loaded because this method is running.
       ' Therefore we don’t have to check if the WebBrowser object is still busy.
       ' It’s time to print…
   End Sub

Access the HTML Stored in the Browser Object


After you have loaded your web site you have access to the underlying HTML. The document object is a property of the web browser object.

           End If
   End Sub