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



The information in this article applies to:

  • Microsoft Internet Explorer (Programming) 6 (SP1)

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.

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.

REFERENCES

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

Modification Type:MajorLast Reviewed:2/25/2004
Keywords:kbCtrl kbWebBrowser kbActivexEvents kbhowto KB836128 kbAudDeveloper