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:

  • 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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.