Web Programming in Visual Basic .NET
| A reader requests expansion of this book to include more material.
You can help by adding new material (learn how) or ask for assistance in the reading room.
| A Wikibookian believes this page should be split into smaller pages with a narrower subtopic.
You can help by splitting this big page into smaller ones. Please make sure to follow the naming policy. Dividing books into smaller sections can provide more focus and allow each one to do one thing well, which benefits everyone.
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 Edit
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.
Load a WebPage into the Browser Object Edit
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. Application.DoEvents() end
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. wbInCode.Navigate("http://www.usatoday.com") End Sub
Print a Web Page from the Browser Object Edit
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… webBrowserTmp.Print() End Sub
Access the HTML Stored in the Browser Object Edit
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 System.Console.WriteLine(tmp) Next End Sub