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)

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

    The Forward button is enabled.
  5. Click Forward to visit http://www.msn.com.
Back to the top

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

311282 HOW TO: Use the WebBrowser Control NewWindow2 Event in Visual Basic .NET


For more information about WebBrowser control, visit the following Microsoft site:
http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/RefList_VB.asp

Back to the top

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005applies kbvs2005swept kbweb KbUIDesign kbWindowsForms kbCtrl kbControl kbBrowse kbHOWTOmaster kbhowto KB811603 kbAudDeveloper