Sofar, you have seen how to apply.Visual Basic to the Web by means of scripting Web pages with VBScript. This chapter demonstrates the merging of the desktop and the Web and includes the related objects and examples. The first topic I’ll discuss is the WebBrowser control, which lets you display HTML documents in yo’:!r VB applications. With this control, you can design a Form that connects the user to your Web site (or a specific page depending on what he or she is doing at the moment) and displays your home page, as if it were viewed with Internet Explorer. Because the WebBrowser control doesn’t provide any navigational tools, you can limit the user to your own Web site (unless the site contains hyperlinks to other sites).
In a corporate environment, you can design Forms that display announcements, special instructions, and all types of information that change frequently. The user doesn’t have to start the browser to view this information. The WebBrowser control enables you to push information to your users from within applications they use daily.
The WebBrowser control, just like Internet Explorer, can display HTML pages, which must be authored ahead of time and reside on a server (or even on your hard disk). It is also possible to manipulate the document in the control directly, through the IE Scripting Object Model. Through the objects of the IE Scripting Object Model, you can write VB applications that generate HTML code and place it on the Web- Browser control. In other words, it is possible to develop VBapplications that generate HTML documents on the fly and display them in the WebBrowser control. It’s an exciting capability, especially for authoring interactive tutorials.The last topic I will discuss in UUschapter relates to custom Activ eX controls. One feature of custom ActiveX controls that I didn’t discuss in Chapter 16 is how to write controls that can download information from the Internet. It is possible to develop ActiveX controls that can take advantage of the host computer’s connection to the Internet and download information from the network, and you’ll see how this can be done in this chapter. These controls are called Internet-enabled, and I expect that many of you will develop custom controls whose primary function is to connect to servers and download information on demand.
Web Browsing Objects
The two objects you need in order to add Web techniques and hyperlinked. documents to your Visual Basic applications are:
• The WebBrowser control
• The InternetExplorer object
The WebBrowser is an ActiveX control that can display HTML documents on Visual Basic Forms. InternetExplorer is an OLE Automation object that you can use to control Microsoft Internet Explorer (and the WebBrowser control) from within your code. The two objects have many common members, and I will discuss them together. The emphasis will be on the WebBrowser control, which VBprogrammers will find more useful.
The WebBrowser Control
Simply put, the WebBrowser control bInternet Explorer’s window. Any H1ML document that can be displayed. in Internet Explorer can also be displayed. in the WebBrowser control. In other words, the WebBrowser control adds browsing capabilities to your Visual Basic applications. It allows the user to browse sites on the World Wide Web, local files, or ActiveX documents, such as Word or Excel documents all from within a VISual Bask application.
Because the WebBrowser is an ActiveX control, you can place it on any VISUal, Basic Form. Before you can use it, however, you must add it to the Toolbox. Follow these steps:
1. Right-click the Toolbox, ar-d from the shortcut menu, select Components to open the Components dialog box.
‘.2. Select Microsoft Internet Co trois, and then click OK. Two new icons will appear on the Toolbox-tbe WebBrowser control’s icon and the She1Folder ViewOC control’s icon. ‘
3. Select the WebBrowser control’s icon and draw ill instance of the control on the Form.
When you place a WebBrowser control on a Form, it’s a borderless rectangle that you can size in any way you like, Because the control can’t be resized. by the user at runtime, you should try to adjust its size according to the size of its container, which is a VISual Basic Form. When the user resizes the Form, the WebBrowser control should be resized also so that it course most of the Form.
To display a Web page in the WebBrowser control, use the Navigate method. You can also move through the list of URis that have been displayed already with the GoBack and GoForward methods. The WebBrowser control automatically maintains the list of visited URLs. We’ll look at the control’s properties, methods, and events later. Let’s start by developing a simple application that demonstrates the basic features of the control.
VB6 at Work: The Browser Project
In this section, we’ll develop an application based on the WebBrowser control that demonstrates how to add Web-browsing capabilities to your Visual Basic applications. The project is called Browser, and you will find it on the book’s.Cl). the Browser application ‘displaying the Sybex Web site. The user can select a URL from the ComboBox control or select a local HTML file by clicking the Open HIML File button.
To build the application, follow these steps:
1. Start a new Standard EXE project, and add the WebBrowser control to the Toolbox, as shown ~arlier in this chapter.
2; Widen the Form and then place an instance of the WebBrowser control on it. Make the control large enough to cover most of the Form’s area
3. Now, place the othercontrols you see in Figure 21.1 on the Form.In addition to the visible controls (the Command buttons and the ComboBox), there is a Common Dialog control, which wiD be used to display the open dialog box and in which the user can select local HTML files to display on the Web browser control. Enter the following lines to initialize the ComboBox control when the Form is loaded:
You can add your favorite URLs here, as long as the name displayed in the box is the name (If a commercial Web server. Wh~ the user selects the 5mJerName entry in the ComboBox,
5. TIleShow URL button creates a complete URL hom the computer’s name and uses it with the Navigate method to display thespedfied URLon the WebDrowser control.
Run the Browser application and check it out. Visit various pages on the World Wide Web, and open HfML files on your local disk (try loading the Web pages developed in the previous chapter). Figure 21.2 shows the Calendar.htm document (which we’ll develop later in this chapter) opened with the Browser application. You can also open other types of documents (images, Word documents, Excel spreadsheets, and so on), as long as the browser can handle them.