devxlogo

Trapping DHTML events from the WebBrowser control

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 libraryPrivate Sub Form1_Load(ByVal sender As System.Object, _    ByVal e As System.EventArgs) Handles MyBase.Load    AxWebBrowser1.Navigate("http://localhost/default.asp")End SubPrivate Sub AxWebBrowser1_NavigateComplete2(ByVal sender As Object, _    ByVal e As AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event) Handles _    AxWebBrowser1.NavigateComplete2    ' must wait for this event to grab a valid refernece to the Document     ' property    Dim doc As mshtml.HTMLDocument = DirectCast(AxWebBrowser1.Document, _        mshtml.HTMLDocument)    ' Cast to the interface that defines the event you're interested in    Dim docevents As mshtml.HTMLDocumentEvents2_Event = DirectCast(doc, _        mshtml.HTMLDocumentEvents2_Event)    ' Define a handler to the onclick event    AddHandler docevents.onclick, AddressOf onclickprocEnd 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

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist