RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB7
Expertise: Intermediate
Aug 31, 2002

Trapping DHTML events from the WebBrowser control

You can host the WebBroweser in a Windows Forms application, similarly to what you can do with any other ActiveX control. You just need to right-click on the control toolbox, select the Customize Toolbox menu command, select the Microsoft Web Browser control from the list of available controls, and click on the OK button. Visual Studio .NET uses the AxImp.exe utility behind the scenes to create a wrapper for the ActiveX control, which appears to the Windows Forms application under the name of "AxWebBrowser."

In general you can use this control as you would use the original WebBrowser control, call its method, assign its properties, trap its events, and so on.

One of the things that aren't easy, though, is trapping the DHTML events that are raised when something happens in the inner HTML page. For example, when the user clicks on an element of the page (or the page's background), that element raises an onclick event that, if not trapped at the element level, bubbles up the DHTML hierarchy until it reaches the top-most Document object. Thus, you can handle any event in the DHTML page at the Document level, even though it's up to you, the programmer, understanding which element on the page raised the event. In VB6 you could manage these events simply by assigning the WebBrowser.Document property to an MSHTML.HTMLDocument variable declared with the WithEvents keyword.

In VB.NET this simple technique doesn't work, though, because the conversion from the ActiveX control creates two interfaces for the control, and these interfaces contains methods with the same name, which in turn confuses the compiler and causes a compile-time error. To have this technique work, you have to explicitly cast the Document object to one of these interfaces and create a non-standard event handler:

' IMPORTANT: this code assumes that you've added a reference to the
'            Microsoft HTML Object Library type library

Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
End Sub

Private Sub AxWebBrowser1_NavigateComplete2(ByVal sender As Object, _
    ByVal e As AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event) Handles _
    ' must wait for this event to grab a valid refernece to the Document 
    ' property
    Dim doc As mshtml.HTMLDocument = DirectCast(AxWebBrowser1.Document, _

    ' Cast to the interface that defines the event you're interested in
    Dim docevents As mshtml.HTMLDocumentEvents2_Event = DirectCast(doc, _
    ' Define a handler to the onclick event
    AddHandler docevents.onclick, AddressOf onclickproc
End Sub

' Notice that the signature of this event is different from usual, as it
' is expected to return a Boolean - if false the default effect associated
' with the event (for example, jumping to another page if the click is on
' an hyperlink) is canceled.

Private Function onclickproc(ByVal obj As mshtml.IHTMLEventObj) As Boolean
    ' an object on the page has been clicked - you can learn more about
    ' type and position of this object by querying the obj's properties
    ' ...
End Function
Francesco Balena
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date