|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Immediate SolutionsCreating A Web BrowserThe Testing Department is calling again. They need a new Web browser program right away. Whats wrong with the old one? you ask. They say, it doesnt display the founders picture. Oh, you say. Its easy to build a Web browser in Visual Basicyou just use the Microsoft WebBrowser control. In this and the next few topics, well put together the functioning Web browser that you see in Figure 21.1. Our browser will support Back, Next, Home, Stop, and Refresh buttons, as you can see in that figure. In addition, the browser will have all the power of the Microsoft Internet Explorer (largely because it is the Internet Explorer; we use the WebBrowser control, which is the Internet Explorer in a control). To let the user navigate, well include a combo box, as you see in Figure 21.1. When the user types a new URL in the combo box and presses the Enter key, well navigate to that URL (and keep a record of the URLs weve been to in the combo boxs drop-down list).
To create our Web browser, follow these steps:
That sets up the Web browserbut how do we work with it in code? Well take a look at that in the next few topics. Specifying URLs In A Web BrowserNow that youve set up the controls well need in a Web browser (see the previous topic), how do you let the user navigate? You use the WebBrowser controls Navigate method. Lets see this at work. For example, when our Web browser first loads, we can navigate to the Microsoft Web page this way (note that you can specify URLs with or without the http:// part in the Internet Explorer, and although we omit it here, you can include that prefix if you prefer):
Private Sub Form_Load()
WebBrowser1.Navigate "www.microsoft.com"
...
End Sub
We also want the user to be able to navigate to a new URL, and thats usually done with a combo box like the one we added to our Web browser in the previous topic, combo1. We start working with combo1 by displaying the present URL and adding it to the combo boxs drop-down list:
Private Sub Form_Load()
WebBrowser1.Navigate "www.microsoft.com"
Combo1.Text = "www.microsoft.com"
Combo1.AddItem Combo1.Text
End Sub
Users can select past URLs from the combo boxs drop-down list. When they do select a URL that way, a Click event is generated, and we can navigate to the newly selected URL this way:
Private Sub Combo1_Click()
WebBrowser1.Navigate Combo1.Text
End Sub
In addition, users can type a new URL into the combo box and press Enter, just as they can in commercial browsers. When they press Enter, we can navigate to the new URL simply by calling the Combo1_Click event handler directly from the KeyPress event handler:
Private Sub Combo1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
Combo1_Click
End If
End Sub
Finally, when the downloading process is complete, the WebBrowser control fires a DownloadComplete event, and we can display the present URL in the browsers title bar, just as any commercial browser might. To do that, we get the browsers present URL from its LocationName property:
Private Sub WebBrowser1_DownloadComplete()
Me.Caption = WebBrowser1.LocationName
...
End Sub
In addition, we can add that URL to the top of the combo boxs list this way:
Private Sub WebBrowser1_DownloadComplete()
Me.Caption = WebBrowser1.LocationName
Combo1.AddItem WebBrowser1.LocationURL, 0
End Sub
And thats itnow the user can navigate around using the combo box. However, we have yet to make all the buttons, such as Back, Forward, and Home, active, and well do that in the next two topics. The code for the browser, browser.frm version 1 (version 2, which is included on the accompanying CD-ROM, will include support for the browser buttons), appears in Listing 21.1.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |