Add a Touch of Web to Your Windows Applications

Add a Touch of Web to Your Windows Applications

mong the controls installed with VB6 is the WebBrowser control, which is nothing less than a complete browser disguised as an ActiveX control (technically, it’s a wrapper around Internet Explorer that can be hosted on a Windows form). The WebBrowser control can contact a remote Web server, download an HTML file and render it onscreen. But in addition to showing HTML documents, the WebBrowser control exposes a Document property, which returns an object that represents the current document. Using the Document property you can access the structure and all the components of a loaded document (the document’s images, links, and so on), or even create a new document on the fly and display it on the control!

You can find the basic information you need to program the WebBrowser control at MSDN’s online WebBrowser Control documentation, so rather than rehash the basics, this article focuses on a few realistic examples of how to use the WebBrowser control in your VB applications. The following figures show three forms that may give you interesting ideas on how you can use the WebBrowser control in your own Windows applications. Figure 1 shows a browser on a Windows form. Users can select a friendly URL from the ComboBox control and the program navigates to the corresponding site and displays the main page on an instance of the WebBrowser control that covers most of the form’s surface. After the document has been completely downloaded, you can click the Scrape Document button to access the HTML document, display its links and other properties, and download all the images referenced in the document and store them in a local folder.

Figure 1: The Online Form. The WebBrowser control on this form allows you to implement a custom browser in VB.

Figure 2 shows the cheapest possible HTML editor you can imagine. The TextBox lets you can enter HTML code. As you can see in the figure, the HTML document can reference images and other remote resources. Because the WebControl exposes the document being rendered as a property, you can configure it to render any arbitrary HTML exposed by your code. This is exactly what the code behind the Render Document button does. It renders the HTML text in the TextBox control by setting the WebBrowser control’s Document property.

Figure 2: The HTMLEditor Form: Using the WebBrowser control to render HTML documents on the fly.

Figure 3 shows a Windows form that uses a Web navigation model to display two related tables. The categories shown in the left pane are the rows of the Categories table from the Northwind database, while the table in the right pane contains detail data about the products in the selected category. The Sales hyperlink in front of each row in the table executes a stored procedure that retrieves sales information (the number of items sold and the total revenue generated for the selected product) from the Northwind database and displays the results in the TextBox controls at the bottom of the form.

Figure 3:The DataBound Form: Adding Web navigational features in your VB applications with the WebBrowser control.

The sample code that accompanies this article consists of a project that contains all three forms.

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:

  • GoBack?equivalent to clicking the browser’s Back button
  • GoForward?equivalent to clicking the browser’s Forward button
  • GoHome and GoSearch?these 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 and Navigate2 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:


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.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist