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

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a