Web Browser In Visual Basic.
If you have the Microsoft Internet Explorer installed, you can build Web browsers using Visual Basic.Microsoft has packaged the Internet Explorer in a control, the WebBrowser control, and we’ll be able to use that control to create a Web browser in this chapter that supports such browser functionality as Back, Forward, Home, Stop, and Refresh buttons. We’ll also let the user specify what URL to navigate to with a combo box as well as keeping track of recently visited URLs in that combo box. Building a Web browser can be a worthwhile project in itself, but another popular use of the WebBrowser control is to add a Web browser to your existing program for added power. In fact, you can use the Web Browser control to open ActiveX documents in a way that makes them look like a seamless part of the program even though that document may have come from the Internet.
Creating A Web Browser
The Testing Department is calling again. They need a new Web browser program right away. What’s wrong with the old one? you ask. They say, it doesn’t display the founder’s picture. Oh, you say. It’s easy to build a Web browser in Visual Basic—you just use the Microsoft WebBrowser control. In
this and the next few topics, we’ll put together the functioning Web browser. Our browser will support Back, Next, Home, Stop, and Refresh buttons,
.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, we’ll include a combo box. When the user types a new URL in the combo box and presses the Enter key, we’ll navigate to that URL (and keep a record of the URLs we’ve been to in the combo box’s drop-down list).
To create our Web browser, follow these steps:
1. Create a new standard Visual Basic project.
2. Select the Project|Components item.
3. Click the Controls tab in the Components dialog box.
4. Select the Microsoft Internet Controls and Microsoft Windows Common Controls entries, and click on OK to close the Components dialog box.
5. Add a WebBrowser control and a toolbar to the form, stretching the WebBrowser control, WebBrowser1, to fill the space under the toolbar.
6. Add five buttons to the toolbar (right-click the toolbar, select the Properties item, click the Buttons tab, and use the Insert Button button to add the buttons).
7. Give the buttons the same captions and Key properties: Back, Next, Home, Stop, and Refresh (for example, the button with the caption “Back” will also have its Key property set to Back so we can identify which button in the toolbar was clicked).
8. Add a combo box, combo1, to the end of the toolbar (draw the combo box in the toolbar to make sure it’s part of the toolbar; don’t double-click to create a combo box and then move it to the toolbar). That sets up the Web browser but how do we work with it in code? We’ll take a look at that in the next few topics.
Specifying URLs In A Web Browser
Now that you’ve set up the controls we’ll need in a Web browser (see the previous topic), how do you let the user navigate?
You use the WebBrowser control’s Navigate method. Let’s 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 that’s 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 box’s 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 box’s 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 browser’s title bar, just as any commercial browser might. To do that, we get the browser’s 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 box’s list this way:
Private Sub WebBrowser1_DownloadComplete()
Me.Caption = WebBrowser1.LocationName
Combo1.AddItem WebBrowser1.LocationURL, 0
End Sub
And that’s it—now 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 we’ll 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).
VERSION 6.00
Object = "{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}#1.1#0"; "SHDOCVW.DLL"
Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 7560
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 7560
StartUpPosition = 3 'Windows Default
Begin ComctlLib.Toolbar Toolbar1
Align = 1 'Align Top
Height = 630
Left = 0
TabIndex = 1
Top = 0
Width = 7560
_ExtentX = 13335
_ExtentY = 1111
ButtonWidth = 1164
ButtonHeight = 953
Appearance = 1
_Version = 393216
BeginProperty Buttons {66833FE8-8583-11D1-B16A-00C0F0283628}
NumButtons = 6
BeginProperty Button1 {66833FEA-8583-11D1-B16A-00C0F0283628}
Caption = "Back"
Key = "Back"
EndProperty
BeginProperty Button2 {66833FEA-8583-11D1-B16A-00C0F0283628}
Caption = "Next"
Key = "Next"
EndProperty
BeginProperty Button3 {66833FEA-8583-11D1-B16A-00C0F0283628}
Caption = "Home"
Key = "Home"
EndProperty
BeginProperty Button4 {66833FEA-8583-11D1-B16A-00C0F0283628}
Caption = "Stop"
Key = "Stop"
EndProperty
BeginProperty Button5 {66833FEA-8583-11D1-B16A-00C0F0283628}
Caption = "Refresh"
Key = "Refresh"
EndProperty
BeginProperty Button6 {66833FEA-8583-11D1-B16A-00C0F0283628}
Style = 4
Object.Width = 100
EndProperty
EndProperty
Begin VB.ComboBox Combo1
Height = 315
Left = 3480
TabIndex = 2
Top = 120
Width = 3975
End
End
Begin SHDocVwCtl.WebBrowser WebBrowser1
Height = 2295
Left = 120
TabIndex = 0
Top = 840
Width = 7335
ExtentX = 12938
ExtentY = 4048
ViewMode = 1
Offline = 0
Silent = 0
RegisterAsBrowser= 0
RegisterAsDropTarget= 1
AutoArrange = -1 'True
NoClientEdge = 0 'False
AlignLeft = 0 'False
ViewID = "{0057D0E0-3573-11CF-AE69-08002B2E1262}"
Location = ""
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Form_Load()
WebBrowser1.Navigate "www.microsoft.com"
Combo1.Text = "www.microsoft.com"
Combo1.AddItem Combo1.Text
End Sub
Private Sub WebBrowser1_DownloadComplete()
Me.Caption = WebBrowser1.LocationName
Combo1.AddItem WebBrowser1.LocationURL, 0
End Sub
Private Sub Combo1_Click()
WebBrowser1.Navigate Combo1.Text
End Sub
Private Sub Combo1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
Combo1_Click
End If
End Sub
Adding Back And Forward Buttons To A Web Browser
Now that we’ve set up a Web browser in which the user can navigate by typing URLs into the combo box (see the previous topic), we’ll enable the Back and Forward buttons in the browser. That’s easier than you might expect—you just use the browser’s GoBack or GoForward methods. We do that like this, where we determine which button in the toolbar has been clicked by checking the keys we’ve added to those buttons:
Private Sub Toolbar1_ButtonClick(ByVal Button As Button)
Select Case Button.Key
Case "Back"
WebBrowser1.GoBack
Case "Forward"
WebBrowser1.GoForward
End Select
End Sub
And that’s all there is to it—now the user can navigate forwards and backwards in the browser’s history. We’ve added Back and Forward buttons now, but the user also expects Refresh, Home, and Stop buttons in Web browsers, and we’ll add those buttons next. Adding Refresh, Home, And Stop Buttons To A Web Browser In the previous few topics, we’ve set up a Web browser complete with combo box to let the user enter and select URLs, as well as a Back and Forward button to let the user navigate through the browser’s history. However, we still have a few more buttons to implement: the Refresh, Home, and Stop buttons. We can implement those buttons with the Web browser control’s Refresh, GoHome, and Stop methods. We’ve given the Refresh, Home, and Stop buttons the keys “Refresh”, “Home”, and “Stop”, so we just call the appropriate Web browser method when the matching button is clicked (note that if the user clicks the Stop button, we also update the current URL as displayed in the browser’s title bar using the Web browser’s LocationName property):
Private Sub Toolbar1_ButtonClick(ByVal Button As Button)
Select Case Button.Key
Case "Back"
WebBrowser1.GoBack
Case "Forward"
WebBrowser1.GoForward
Case "Refresh"
WebBrowser1.Refresh
Case "Home"
WebBrowser1.GoHome
Case "Stop"
WebBrowser1.Stop
Me.Caption = WebBrowser1.LocationName
End Select
End Sub
If you have the Microsoft Internet Explorer installed, you can build Web browsers using Visual Basic.Microsoft has packaged the Internet Explorer in a control, the WebBrowser control, and we’ll be able to use that control to create a Web browser in this chapter that supports such browser functionality as Back, Forward, Home, Stop, and Refresh buttons. We’ll also let the user specify what URL to navigate to with a combo box as well as keeping track of recently visited URLs in that combo box. Building a Web browser can be a worthwhile project in itself, but another popular use of the WebBrowser control is to add a Web browser to your existing program for added power. In fact, you can use the Web Browser control to open ActiveX documents in a way that makes them look like a seamless part of the program even though that document may have come from the Internet.
Creating A Web Browser
The Testing Department is calling again. They need a new Web browser program right away. What’s wrong with the old one? you ask. They say, it doesn’t display the founder’s picture. Oh, you say. It’s easy to build a Web browser in Visual Basic—you just use the Microsoft WebBrowser control. In
this and the next few topics, we’ll put together the functioning Web browser. Our browser will support Back, Next, Home, Stop, and Refresh buttons,
.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, we’ll include a combo box. When the user types a new URL in the combo box and presses the Enter key, we’ll navigate to that URL (and keep a record of the URLs we’ve been to in the combo box’s drop-down list).
To create our Web browser, follow these steps:
1. Create a new standard Visual Basic project.
2. Select the Project|Components item.
3. Click the Controls tab in the Components dialog box.
4. Select the Microsoft Internet Controls and Microsoft Windows Common Controls entries, and click on OK to close the Components dialog box.
5. Add a WebBrowser control and a toolbar to the form, stretching the WebBrowser control, WebBrowser1, to fill the space under the toolbar.
6. Add five buttons to the toolbar (right-click the toolbar, select the Properties item, click the Buttons tab, and use the Insert Button button to add the buttons).
7. Give the buttons the same captions and Key properties: Back, Next, Home, Stop, and Refresh (for example, the button with the caption “Back” will also have its Key property set to Back so we can identify which button in the toolbar was clicked).
8. Add a combo box, combo1, to the end of the toolbar (draw the combo box in the toolbar to make sure it’s part of the toolbar; don’t double-click to create a combo box and then move it to the toolbar). That sets up the Web browser but how do we work with it in code? We’ll take a look at that in the next few topics.
Specifying URLs In A Web Browser
Now that you’ve set up the controls we’ll need in a Web browser (see the previous topic), how do you let the user navigate?
You use the WebBrowser control’s Navigate method. Let’s 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 that’s 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 box’s 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 box’s 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 browser’s title bar, just as any commercial browser might. To do that, we get the browser’s 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 box’s list this way:
Private Sub WebBrowser1_DownloadComplete()
Me.Caption = WebBrowser1.LocationName
Combo1.AddItem WebBrowser1.LocationURL, 0
End Sub
And that’s it—now 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 we’ll 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).
VERSION 6.00
Object = "{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}#1.1#0"; "SHDOCVW.DLL"
Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 7560
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 7560
StartUpPosition = 3 'Windows Default
Begin ComctlLib.Toolbar Toolbar1
Align = 1 'Align Top
Height = 630
Left = 0
TabIndex = 1
Top = 0
Width = 7560
_ExtentX = 13335
_ExtentY = 1111
ButtonWidth = 1164
ButtonHeight = 953
Appearance = 1
_Version = 393216
BeginProperty Buttons {66833FE8-8583-11D1-B16A-00C0F0283628}
NumButtons = 6
BeginProperty Button1 {66833FEA-8583-11D1-B16A-00C0F0283628}
Caption = "Back"
Key = "Back"
EndProperty
BeginProperty Button2 {66833FEA-8583-11D1-B16A-00C0F0283628}
Caption = "Next"
Key = "Next"
EndProperty
BeginProperty Button3 {66833FEA-8583-11D1-B16A-00C0F0283628}
Caption = "Home"
Key = "Home"
EndProperty
BeginProperty Button4 {66833FEA-8583-11D1-B16A-00C0F0283628}
Caption = "Stop"
Key = "Stop"
EndProperty
BeginProperty Button5 {66833FEA-8583-11D1-B16A-00C0F0283628}
Caption = "Refresh"
Key = "Refresh"
EndProperty
BeginProperty Button6 {66833FEA-8583-11D1-B16A-00C0F0283628}
Style = 4
Object.Width = 100
EndProperty
EndProperty
Begin VB.ComboBox Combo1
Height = 315
Left = 3480
TabIndex = 2
Top = 120
Width = 3975
End
End
Begin SHDocVwCtl.WebBrowser WebBrowser1
Height = 2295
Left = 120
TabIndex = 0
Top = 840
Width = 7335
ExtentX = 12938
ExtentY = 4048
ViewMode = 1
Offline = 0
Silent = 0
RegisterAsBrowser= 0
RegisterAsDropTarget= 1
AutoArrange = -1 'True
NoClientEdge = 0 'False
AlignLeft = 0 'False
ViewID = "{0057D0E0-3573-11CF-AE69-08002B2E1262}"
Location = ""
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Form_Load()
WebBrowser1.Navigate "www.microsoft.com"
Combo1.Text = "www.microsoft.com"
Combo1.AddItem Combo1.Text
End Sub
Private Sub WebBrowser1_DownloadComplete()
Me.Caption = WebBrowser1.LocationName
Combo1.AddItem WebBrowser1.LocationURL, 0
End Sub
Private Sub Combo1_Click()
WebBrowser1.Navigate Combo1.Text
End Sub
Private Sub Combo1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
Combo1_Click
End If
End Sub
Adding Back And Forward Buttons To A Web Browser
Now that we’ve set up a Web browser in which the user can navigate by typing URLs into the combo box (see the previous topic), we’ll enable the Back and Forward buttons in the browser. That’s easier than you might expect—you just use the browser’s GoBack or GoForward methods. We do that like this, where we determine which button in the toolbar has been clicked by checking the keys we’ve added to those buttons:
Private Sub Toolbar1_ButtonClick(ByVal Button As Button)
Select Case Button.Key
Case "Back"
WebBrowser1.GoBack
Case "Forward"
WebBrowser1.GoForward
End Select
End Sub
And that’s all there is to it—now the user can navigate forwards and backwards in the browser’s history. We’ve added Back and Forward buttons now, but the user also expects Refresh, Home, and Stop buttons in Web browsers, and we’ll add those buttons next. Adding Refresh, Home, And Stop Buttons To A Web Browser In the previous few topics, we’ve set up a Web browser complete with combo box to let the user enter and select URLs, as well as a Back and Forward button to let the user navigate through the browser’s history. However, we still have a few more buttons to implement: the Refresh, Home, and Stop buttons. We can implement those buttons with the Web browser control’s Refresh, GoHome, and Stop methods. We’ve given the Refresh, Home, and Stop buttons the keys “Refresh”, “Home”, and “Stop”, so we just call the appropriate Web browser method when the matching button is clicked (note that if the user clicks the Stop button, we also update the current URL as displayed in the browser’s title bar using the Web browser’s LocationName property):
Private Sub Toolbar1_ButtonClick(ByVal Button As Button)
Select Case Button.Key
Case "Back"
WebBrowser1.GoBack
Case "Forward"
WebBrowser1.GoForward
Case "Refresh"
WebBrowser1.Refresh
Case "Home"
WebBrowser1.GoHome
Case "Stop"
WebBrowser1.Stop
Me.Caption = WebBrowser1.LocationName
End Select
End Sub
0 comments:
Post a Comment
Dear Visitors All The Tricks And Hacks Posted Here Are Only For Knowledge Purpose.Don't Use These for Illegal Operations.