Using the WebBrowser Control
To use the WebBrowser control on a form, you must first add it to the Toolbox. Right-click the Toolbox and select Components. In the dialog box that appears, select the Microsoft Internet Control component. While you're at it, add the Internet Transfer Control (INet) as well, which the sample project also uses. You'll see more about the INet Control later in this article.
To navigate to a URL, use the control's Navigate
method, which accepts the desired URL as an argument. As soon as you call the Navigate
method, the control fires the BeforeNavigate
event and then starts downloading and rendering the corresponding document. You can determine when the download completes by checking the control's Busy
property, which returns True
as long as the control is still downloading the document. Other navigational methods you can use are:
- GoBackequivalent to clicking the browser's Back button
- GoForwardequivalent to clicking the browser's Forward button
- GoHome and GoSearchthese navigate to the home and search pages, as specified in Internet Explorer's property pages.
In addition to the Navigate
method, there's a similarly named method called Navigate2
. The difference is that the Navigate2
method's argument can accept a local file name as well as a URL. Both the Navigate
methods accept other, optional arguments that allow you to specify additional information about the navigation, such as the name of the frame in which the browser will render the requested document, and the HTTP headers it should send to the server.
When a requested document download completes, the control fires the NavigateComplete2
event. You can use this event to enable the appropriate buttons on your form. The Refresh
method reloads the document currently displayed on the control's surface. A Refresh2
method does the same, but also accepts an argument that determines the refresh level
. This refresh level argument can have one of the following values:
- REFRESH_NORMAL 0
- REFRESH_IFEXPIRED 1
- REFRESH_COMPLETELY 3
Finally, to cancel any navigation or download operation in process, you can call the Stop
method, which also halts any dynamic page elements, such as background sounds and animations.
The WebBrowser's Document Property
From a programming viewpoint, the most important property of the WebBrowser control is the Document
property, which represents the currently loaded document. The Document
property returns a reference to a complex object with its own object model. The Document object's basic properties expose the contents of the HTML document. That's extremely powerful, because you can programmatically explore or modify any portion of the document dynamically.
For example, the Links property is a collection of Link objects that represent the document's hyperlinks. The Document.Links.Length property returns the total number of links in the document. Each item in of the Links collection represents a different link in the document. The Link object's href property contains the link URL. For example, Document.Links(0).href is the URL of the first link in the document, Document.Links(0).href is the URL of the second link, and so on.
Likewise, the Images Property is a collection of objects that represent the images referenced in the document. Although you can't access the bitmaps directly as Image objects, you can retrieve the URL of each image and download it with the INet control.
The Forms Property is a collection of Form objects that represent the various tags in the document. You can use the Forms property to access the individual controls on each form and read or modify their values from within your code. For example, you can use this property to find out the values of form controls before the browser submits the form to a server (use the BeforeNavigate2 event to detect form submission), to discover the values of hidden controls on the form, or to set the values of the form controls and submit the form programmatically.