Browse DevX
Sign up for e-mail newsletters from DevX


Add a Touch of Web to Your Windows Applications

Learn to use the WebBrowser control to add Web-style navigational features to your Windows applications and access files on remote Web servers.


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.

Thanks for your registration, follow us on our social networks to keep up-to-date