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 Basic .NET and in Visual C# .NET


View products that this article applies to.

Introduction

When you host the WebBrowser control, you may want to implement a Forward button and a Back button. These buttons are similar to buttons that Microsoft Internet Explorer implements. This article explains the elements and the code that are used with the WebBrowser control to implement these buttons.

↑ Back to the top


More information

To overcome this problem, you can use the WebBrowser control with the CommandStateChange event. The CommandStateChange event is fired whenever the Forward button or the Back button must be either enabled or disabled. The CSC_NAVIGATEFORWARD constant and the CSC_NAVIGATEBACK constant indicate the type of action that the user wants.These constants are defined in the CommandStateChangeConstants enumerator. This enumerator is a member of the SHDocVw dynamic-link library (DLL).

Microsoft Visual Basic .NET sample code

Following is the Visual Basic .NET sample code that handles the CommandStateChange event.
' Visual Basic .NET sample code for handling the CommandStateChange event

Private Sub AxWebBrowser1_CommandStateChange(ByVal sender As Object, 
		ByVal e As AxSHDocVw.DWebBrowserEvents2_CommandStateChangeEvent) Handles AxWebBrowser1.CommandStateChange
        Select Case CType(e.command, CommandStateChangeConstants)
            Case CommandStateChangeConstants.CSC_NAVIGATEBACK
                btnBack.Enabled = e.enable
            Case CommandStateChangeConstants.CSC_NAVIGATEFORWARD
                btnForward.Enabled = e.enable
        End Select        
End Sub

Microsoft Visual C# .NET sample code

Following is the Visual C# .NET sample code that handles the CommandStateChange event.
// Visual C# .NET sample code for handling the CommandStateChange event

private void axWebBrowser1_CommandStateChange(object sender, 
		AxSHDocVw.DWebBrowserEvents2_CommandStateChangeEvent e)
{
	switch (CommandStateChangeConstants)e.command
	{
		case CommandStateChangeConstants.CSC_NAVIGATEBACK:
			btnBack.Enabled=e.enable;
			break;
		case CommandStateChangeConstants.CSC_NAVIGATEFORWARD:
			btnForward.Enabled=e.enable;
			break;
		case default:
			break;
	}
}
Note The control that is used for the Forward button is the btnForward control. The control that is used for the Back button is the btnBack control.

↑ Back to the top


References

For more information about the WebBrowser object, visit the following Web site:

↑ Back to the top


Properties

Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

↑ Back to the top


Keywords: KB836128, kbhowto, kbactivexevents, kbwebbrowser, kbctrl

↑ Back to the top

Article Info
Article ID : 836128
Revision : 2
Created on : 5/17/2007
Published on : 5/17/2007
Exists online : False
Views : 428