Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to use Forward and Back buttons for the WebBrowser control in Visual C# .NET


View products that this article applies to.

Summary

When you host the WebBrowser control and you want to implement Forward and Back buttons that are similar to those that Microsoft Internet Explorer implements, it is important to know when to enable and when to disable the buttons.

The WebBrowser control supports a CommandStateChange event, which fires whenever you must enable or disable the Forward or Back buttons. The CommandStateChange event is sent with two parameters: a constant that indicates the type of button (CSC_NAVIGATEFORWARD or CSC_NAVIGATEBACK), and a Boolean flag that indicates whether to enable or disable the button. CSC_NAVIGATEFORWARD and CSC_NAVIGATEBACK are defined in the Exdisp.h header topic, which comes with the Internet Client software developer kit (SDK).

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs required to enable the Forward and Back buttons:
  • Microsoft Visual Studio .NET.
  • Host WebBrowser control (Shdocvw.dll).
  • Microsoft Internet Explorer (Programming) version 5.5 or later.

Host the WebBrowser Control in Visual C# .NET

  1. Open Visual Studio .NET. Create a new Visual C# .NET Windows application.
  2. On the Project menu, click Add reference, and then click the COM tab in the upper left part of the window.
  3. Browse the list of Component Object Model (COM) components. Double-click Microsoft Internet Controls and Microsoft HTML Object Library, and then click OK.
  4. In the toolbox, right-click the Components tab, and then click Customize toolbox. A small dialog box appears. By default, the COM tab is selected. Select Microsoft Web Browser, (which points to Shdocvw.dll in your System32 directory), and then click OK.
  5. A new control named Explorer appears on the Components tab of your toolbox. This is the WebBrowser control that you can put on the form.
NOTE For this project to work correctly, you must set references to Microsoft HTML Object Library and to Microsoft Internet Controls. If the code does not run, verify that these two components are selected by reviewing steps 1 through 3 of this section of the article.

Steps to Create Forward and Back Buttons for a WebBrowser Control in Visual C# .NET

  1. Put an instance of the WebBrowser control on the form. By default, the form is named axWebBrowser1.
  2. Put two buttons on the Windows form. Change the text for one button to Forward. Change the text for the other button to Back. Name the form ID for the Back button btnBack, and then name the form ID for the Forward button btnForward.
  3. Add the two directives: using mshtml; and using SHDocVw; at the top of the Form1.cs file.
  4. Use Design mode of the Windows form application, and then click the WebBrowser control. Inside the properties window (by default, it is located at the lower-right of the screen), click the Events icon. Search for the CommandStateChangeevent , and then double-click the blank area to the right of the event name.
  5. Insert the following code in the event handler.
    private void axWebBrowser1_CommandStateChange(object sender, AxSHDocVw.DWebBrowserEvents2_CommandStateChangeEvent e)
    {
    	if ((CommandStateChangeConstants) e.command  == CommandStateChangeConstants.CSC_NAVIGATEBACK)
    	{
    		btnBack.Enabled = e.enable;
    	}
    	else if ((CommandStateChangeConstants) e.command == CommandStateChangeConstants.CSC_NAVIGATEFORWARD)
    	{
    		btnForward.Enabled = e.enable;
    	}
    }
    						
  6. Return to Design mode, double-click the Back button, and then insert the following code to its click event.
    private void btnBack_Click(object sender, System.EventArgs e)
    {
    	mshtml.HTMLDocument doc = new HTMLDocumentClass();
    	mshtml.HTMLWindow2  win;
    
    	doc = (HTMLDocument) axWebBrowser1.Document;
    	win = (HTMLWindow2) doc.parentWindow;
    
    	object o = -1;
            // win.history.back() should work too.
    	win.history.go(ref o);
    }
    						
  7. Repeat the previous procedure, and this time double-click the Forward button. Insert the following code to its click event.
    private void btnForward_Click(object sender, System.EventArgs e)
    {
    	mshtml.HTMLDocument doc = new HTMLDocumentClass();
    	mshtml.HTMLWindow2  win;
    
    	doc = (HTMLDocument) axWebBrowser1.Document;
    	win = (HTMLWindow2) doc.parentWindow;
    
    	object o = 1;
            // win.history.forward() should work too.
    	win.history.go(ref o);
    }
    						
  8. To confirm whether the buttons work properly, visit the following Microsoft Web site: http://www.microsoft.com. Click some of the links to confirm that the buttons work properly.

↑ Back to the top


References

For more information, click the following article number to view the article in the Microsoft Knowledge Base:
163282 How to use Forward and Back buttons for WebBrowser control

↑ Back to the top


Keywords: KB330280, kbhowtomaster

↑ Back to the top

Article Info
Article ID : 330280
Revision : 4
Created on : 4/18/2006
Published on : 4/18/2006
Exists online : False
Views : 487