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.

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:

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.

Print a Web Page from the Browser Object
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.

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.