What makes Web pages tick is the hypertext mode! they use to connect to other pages, anywhere on the World Wide Web. Although you can access the functionality of Web technology from within your Visual Basic applications with objects such as the WebBrowser and InternetExplorer, you still can’t exploit this tecl:nology by making itan integral part of you Visual Basic application. The WebBrowser controls-make it possible to exploit the hyperlink model in your applications, and we’ll present an example that uses hyperlinks as part of . the user interface of a Visual Basic application.
VB6 at Work: The DemoPage Project
The DemoPage application is shown in Figure 21.5, and you will find it on this book’s CD. The DemoPage application consists of two Porms.
The main Form is VBFQAnand is used for drawing simple shapes with VISual Basic methods. The WebForm displays an HTML document that contains instructions on the Visual Basic drawing methods. The HTML document contains the instructions and a few;yperlinks. When either hyperlink is activated, it doesn’t display another document. Instead, it draws a shape on the first VIsual Basic Form. Design the two Forms ‘as shown in Figure 21.5. The main Form contains a Label: control at the top, on which a command is displayed. The second Form (Web- Form) contains a WebBrowser control, on which the Demo.htm page is displayed. When the first Form is loaded, it loads the second Form and displays the HrML document on the WebBrowser control. All the code in the VBForm Form is located in the Load event.
To avoid an absolute reference, the code assumesthat the HTML document (the file Demo.htm) is stored in the same folder as the project. The. complete Demo.htm page is shown in Figure 21.6
As you may have guessed, the application exploits the BeforeNavip event to find out which hyperfirik was activated and then cancelI the jump to this hyperlink and does something on the first Form (displays the command in the Label control and draws a shape). The hyperlinks could be fake; all we need is to know which one was clicked, However, the BeforeNavigate2 event isn’t triggered unless the destination of the hyperlink is a valid URL..
‘nle ~tions of the two hyperlinks are shown next. As you can see, the destinations of the hyperlinks include some information about the kind of shape to be drawn on the first Form.
The HTML document doesn’t contain any anchors named “circle” and “box”, and you don’t really need them. The WebBrowser control generates an error message, but all you really need is the name of the anchor. The server address is the IP address of the local machine (127.0.0.1), which is always a valid server name. Let’s see how the code of the BeforeNavigate2 event causes some action to take place on the other Form.
<A HREF = “http://127.0.0.1/demo.htm#circle”>Draw A Circle</A>
<A HREF = “http://127.0.0.1/demo.htm#box”>Draw A Square</A>
The BeforeNavigate2 Event
The first statement is an error-trapping statement; it tells Visual Basic to ignore errors and continue with the next statement. We know that an error will occur because the destinations of the two hyperlinks are invalid. We then cancel the navigation by setting the Cancel argument to True. The H statement makes sure that other (possibly valid) hyperlinks aren’t canceled. The program then examines the last part of the hyperlink’s destination URL (everything to the right of the pound sign). H this string is “circle”, the program draws a circle on the VBForm Form and displays the command used to draw the circle in the Label control. H the string is “box”, it draws a square <?nthe Form and displays the corresponding command on the Label. Youcan easily modify this application to accommodate more actions, place detailed instructions in the HTML document, and even create demos for your applications. The approach is rather clumsy, but hyperlinks are not yet part of the Visual Basic interface model. The application does, however, demonstrate how to incorporate the functionality of hyperlinks in your VISualBasic applications.