Microsoft KB Archive/811603

= How to use the Forward button and the Back button for WebBrowser control in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 811603

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Internet Explorer (Programming)

-



IN THIS TASK

 * SUMMARY
 * CommandStateChange Event
 * Create a Sample Application
 * Add the Functionality for Forward and Back Buttons
 * Test the Application
 * REFERENCES



SUMMARY
This 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 Event
The 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 top

Create a Sample Application
To create a sample application, follow these steps:
 * 1) 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.
 * 2) On the Tools menu, click Customize Toolbox.

Note In Visual Studio 2005, click Choose Toolbox Items.
 * 1) On the COM Components list, click Microsoft Web Browser, and then click OK.
 * 2) From the toolbox, double-click Panel, right-click Panel1, and then click Properties.
 * 3) In the Properties window, change the Dock property to Top.
 * 4) Double-click to expand the Size property. Change the Height property to 40.
 * 5) From the toolbox, drag a Label to Panel1. Change the Text property of the Label to Address.
 * 6) From the toolbox, drag a TextBox to Panel1 next to Address. Change the Text property to http://www.microsoft.com.
 * 7) 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.
 * 8) 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.
 * 9) 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.
 * 10) From the toolbox, drag Explorer below Panel1 to Form1. Change the Dock property of AxBrowser1 to Fill.
 * 11) On the File menu, click Save All.

Back to the top

Add the Functionality for Forward and Back Buttons
To 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.</li>  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 </li></ul> </li> On the File menu, click Save All.</li></ol>

Back to the top

Test the Application
To test the application, follow these steps:
 * 1) On the Debug menu, click Start.
 * 2) To visit the URL that is specified in TextBox, click the GO button.
 * 3) Type Http://www.msn.com in the TextBox, and then click the GO button.

The Back button is enabled.
 * 1) Click Back to return to http://www.microsoft.com.

The Forward button is enabled.
 * 1) Click Forward to visit http://www.msn.com.

Back to the top

<div class="references_section">