You can use the WebBrowser control to build customized Web browsers, because it supports all the browsing functionality of Internet Explorer. You can implement the Back and Forward buttons of Internet Explorer with the GoBack and GoForward methods, capture the jumps to hyperlinks, control which sites the user of the application can visit, and so on.Of course, the WebBrowser doesn’t have all the features of Internet Explorer. The most important limitation is that it can’t access the displayed ‘document, and you can’t save the current HTML document from within your code. The user, however, can invoke the document’s shortcut menu and select View Source. In the following two sections, we are going to look at two applications that demonstrate how to use the WebBrowser control from within Visual Basic The first application is a custom Web browser. The second application demonstrates how to exploit the hypertext model of an HTML document from vvithin Visual Basic applications and how to add hyperlink features so a user interface.
VBG at Work: A Customize Web Browser
Figure 21.4shows an interesting ?.p, r ch to customized browsers. The Form shown in this figure contains a URL. The URLs can be local fle~ cr p’ 0″‘:. on r U,G scr-ers.Y 1] can local help files for an application (step-by-step instructions, can if application’s users to aWeb server that information.
To create the Superbrowser application, follow these steps:
1. Start a new Standard EXE project, and add a TabSt ‘p control to the Form. ‘
Make the Form larger than its default size, and stretch the TabStrip control to fill as’much of the Form as possible. You may wantto leave some space for a few command buttons, such as Back and Forward (which aren’t implemented in this example).
2. If the Toolbox doesn’t contain the WebBrowser icon, add it using the Components dialog box (select Internet Controls from the list of available components). You may have to add the TabStrip control to-the Toolbox using the Com~nents dialog box also. ‘
3. In the Toolbox, select the WebBrowser control, and place an instance of the control on each of the TabStrip’s tabs. The WebBrowser control should cover nearly’ all the tab’s area. The three WebBrowser controls are members of the control array WebBrowserl, and their Index values are 0, I, and 2. 4. Double-elide the Form to open its Code window.
5. In the Form’s Load event, enter the following lines, which cause the three WebBrowser controls to navigate to three different Web sites:
Private Sub Form_Load()
WebBrowserl(O).Navigate ‘http://home~microsoft.com·
WebBrowserl(l).Navigate .http://www.sybex.com.
WebBrowserl(2).Navigate .http://www.infoseek.com.
End Sub
As soon as the Form is loaded, the corresponding WebBrowser controls download the three pages and display them progressively. You can select the page to view by switching to the appropriate tab of the TabStrip control. All three pages continue downloading, as if you had opened three instances of Internet Explorer, each displaying a different document. Let’s add a few lines of code to display the TJRL of each page on the corresponding tab. Switch back to the Code window, and enter the following line in the Web- Browser control’ s BeforeNavigate2 event:
This line displays the URL of the page that starte •.•iownloading to the corresponding tab’s caption area.
Once the page is downloaded,’ the WebBrowser control’knows its title. At this point, you replace the URL with the actual title. Enter the code to do so in the NavigateComplete event:
Now run the application and watch how the captions on the TabStrip control reflect the contents of each page
Monitoring the Download Progress
This application provides a good opportunity to experiment with the download events. Switch back to the Code window, and enter the-following code in the Downloadbegin and DownloedComplete events:
Private Sub WebBrowser 1_DownloadBegin (Index As Integer)
Debug.Print ‘Started Download for tab #’ & Index
End Sub
If you run the application now, you will see tee following messages in the Immediate execution window (the order will be different on your computer):
The WebBrowser control starts and completes several downloads for each page. These messages correspond to the downloads of the various elements of each page. If you want to display the progress as well, you must program the Progress Change event.
To do so, place a Label control on the first tab of the TabStrip control, and enter the following lines in the We~Browser control’s ProgressChange event:
The outer If structu.re makes sure that only the progress of the selected tab is displayed. If the Progresstlhenge event reports the progress of a WebBrowser. control other than the one, on the currently active tab, the event is not processed.