Microsoft KB Archive/163282

From BetaArchive Wiki
Knowledge Base


How To Use Forward and Back Buttons for WebBrowser Control

Article ID: 163282

Article Last Modified on 7/13/2004



APPLIES TO

  • Microsoft Internet Explorer 3.0
  • Microsoft Internet Explorer 5.0
  • Microsoft Internet Explorer 5.5



This article was previously published under Q163282

SUMMARY

When hosting the WebBrowser control, it may be desirable to implement Forward and Back buttons similar to those that Internet Explorer implements. One common problem that programmers face when doing this is how to know when to enable and disable the buttons.

MORE INFORMATION

The WebBrowser control supports a CommandStateChange event, which is fired whenever the Forward or Back buttons need to be enabled or disabled. The CommandStateChange event is sent with two parameters: a constant indicating the type of button (CSC_NAVIGATEFORWARD or CSC_NAVIGATEBACK), and a Boolean flag indicating whether to enable or disable the button. CSC_NAVIGATEFORWARD and CSC_NAVIGATEBACK are defined in Exdisp.h, which comes with the Internet Client SDK.

// For an MFC application the CommandStateChange event could be handled
// as follows:

#include <exdisp.h>

void CBrowserDlg::OnCommandStateChangeExplorer1(long Command, BOOL Enable)
{
    switch(Command)
    {
     case CSC_NAVIGATEFORWARD:
        // m_ctlForward is a CButton type
        m_ctlForward.EnableWindow(Enable);
        break;

     case CSC_NAVIGATEBACK:
        m_ctlBack.EnableWindow(Enable);
        break;

     default:
        break;
     }
}   

' A Visual Basic application can also implement this
' functionality in this manner:   

Private Sub WebBrowser_CommandStateChange(ByVal Command As Long,ByVal Enable As Boolean)

       Select Case Command
           Case CSC_NAVIGATEBACK
               Back.Enabled = Enable
           Case CSC_NAVIGATEFORWARD
               Forward.Enabled = Enable
       End Select

End Sub
                

Keywords: kbhowto kbusage KB163282