How to use the Forward button and the Back button for WebBrowser control in Visual Basic .NET or in Visual Basic 2005 (811603)
The information in this article applies to:
- Microsoft Visual Basic 2005
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
- Microsoft Internet Explorer (Programming)
SUMMARYThis step-by-step article describes how to enable and
disable the Forward button and the Back button in a WebBrowser control. When you host the WebBrowser
control, implement Forward and Back buttons that are similar to those implemented in Microsoft
Internet Explorer. You may not know when to enable and disable the buttons
while you implement them. Back to the top CommandStateChange EventThe WebBrowser control supports a CommandStateChange event. This event is fired whenever the Forward or Back buttons must be enabled or disabled. The CommandStateChange event is sent with the following parameters:
- A constant that indicates the type of button
(CSC_NAVIGATEFORWARD or CSC_NAVIGATEBACK).
- A Boolean flag that indicates whether to enable or disable
the button.
Back to
the topCreate a Sample ApplicationTo create a sample application, follow these steps:
- Open Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. Create a new Windows
Application project by using Microsoft Visual Basic .NET or Microsoft Visual Basic 2005.
- On the Tools menu, click Customize
Toolbox.
Note In Visual Studio 2005, click Choose Toolbox Items. - On the COM Components list, click
Microsoft Web Browser, and then click
OK.
- From the toolbox, double-click Panel,
right-click Panel1, and then click
Properties.
- In the Properties window, change the
Dock property to Top.
- Double-click to expand the Size property.
Change the Height property to
40.
- From the toolbox, drag a Label to Panel1. Change the Text
property of the Label to Address.
- From the toolbox, drag a TextBox to Panel1 next to Address.
Change the Text property to
http://www.microsoft.com.
- From the toolbox, drag a Button to Panel1 next to TextBox. Change the Name property of the button to
cmdGo, and change the Text property to
&GO.
- From the toolbox, drag another Button to Panel1 next to the GO
button. Change the Name property of the button to
cmdBack, and change the Text property
to &Back. Set the Enable property
to False.
- From the toolbox, drag another Button to Panel1 next to the Back
button. Change the Name property of the button to
cmdForward and the Text property to
&Forward. Set the Enable property
to False.
- From the toolbox, drag Explorer below
Panel1 to Form1. Change the
Dock property of AxBrowser1 to
Fill.
- On the File menu, click Save
All.
Back to
the top Add the Functionality for Forward and Back ButtonsTo add the functionality for Forward and Back buttons, follow these steps:
- In the designer pane, double-click the GO
button to view the implementation of the cmdGo_Click event. Append the following code to Class Form1 so that you can
navigate to the URL specified in TextBox1:
Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click
' Go to the URL specified in Textbox1
AxWebBrowser1.Navigate(TextBox1.Text)
End Sub - In the designer pane, double-click the
Back button to view the implementation of the cmdBack_Click event. Append the following code to Class Form1 so that you can
navigate to the previous URL when you click the Back button:
Private Sub cmdBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBack.Click
' Go to the Previous URL when the Back button is clicked
AxWebBrowser1.GoBack()
End Sub - In the designer pane, double-click the
Forward button to view the implementation of the cmdForward_Click event. Append the following code to Class Form1 so that you can
navigate to the next URL when you click the Forward button:
Private Sub cmdForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdForward.Click
' Go to the Next URL when the Forward button is clicked
AxWebBrowser1.GoForward()
End Sub - Append the following code to the variable declaration
section of the Class Form1:
Private Const CSC_NAVIGATEFORWARD As Integer = 1
Private Const CSC_NAVIGATEBACK As Integer = 2 - To add code to the CommandStateChange event of AxWebBrowser1, follow these steps:
- Select AxWebBrowser1 from the
Class Name list.
- Select CommandStateChange from the
Method Name list.
- Append the following code to the Class Form1 for the CommandStateChangeevent:
Private Sub AxWebBrowser1_CommandStateChange(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_CommandStateChangeEvent) Handles AxWebBrowser1.CommandStateChange
' e.command identifies the effected button
Select Case e.command
Case CSC_NAVIGATEBACK 'Back button
' Enable OR Disable the Back button
cmdBack.Enabled = e.enable
Case CSC_NAVIGATEFORWARD 'Forward button
' Enable OR Disable the Forward button
cmdForward.Enabled = e.enable
End Select
End Sub
- On the File menu, click Save
All.
Back to
the top Test the ApplicationTo test the application, follow these steps:
- On the Debug menu, click
Start.
- To visit the URL that is specified in TextBox, click the GO button.
- Type Http://www.msn.com in the
TextBox, and then click the GO button.
The Back button is enabled. - Click Back to return to
http://www.microsoft.com.
The Forward button is enabled. - Click Forward to visit
http://www.msn.com.
Back to
the top
Modification Type: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbvs2005applies kbvs2005swept kbweb KbUIDesign kbWindowsForms kbCtrl kbControl kbBrowse kbHOWTOmaster kbhowto KB811603 kbAudDeveloper |
---|
|