Microsoft KB Archive/312777

= How to handle document events in a Visual C# .NET application =

Article ID: 312777

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q312777





For a Microsoft Visual Basic .NET version of this article, see 311284.

IN THIS TASK
SUMMARY
 * Requirements
 * Description of the technique
 * Create the project, and then add code
 * Notes

REFERENCES



SUMMARY
This article describes how to catch document events for the WebBrowser control in Microsoft Visual C# .NET.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Microsoft Visual Studio .NET
 * Internet Explorer 5.5 Service Pack 2 (SP2) or later version

This article also assumes that you are familiar with the following products:
 * Visual Studio .NET
 * Internet Explorer
 * The WebBrowser control

back to the top

Description of the technique
The WebBrowser control is easy to work with in Microsoft Visual Studio or Visual Studio .NET. But handling the events of the WebBrowser control in Visual Studio .NET may be confusing at first. The events themselves are exposed by the Mshtml.HTMLDocumentEvents2_Event event interface. This interface exposes most of the document events that must be handled in your application.

To handle the event, you must create your own function that you can call when the event occurs. You must match the signature of the event that fires. For example, the following function handles the MouseOver event of the document: private void MouseOverEventHandler(mshtml.IHTMLEventObj e) After the event handler is in position, you must hook the event. You can hook an event at any time after the DocumentComplete event on the WebBrowser control is triggered. To connect the sender of the event to your event handler, you must set up a delegate. Delegates are type-safe, security-enhanced, managed objects that point to a method. The following code example demonstrates how to connect the MouseOverEventHandler method to the MouseOver event of the document: mshtml.HTMLDocumentEvents2_Event iEvent; iEvent.onmouseover += new mshtml.HTMLDocumentEvents2_onmouseoverEventHandler(MouseOverEventHandler); back to the top

Create the project, and then add code
The following code sample sends the WebBrowser control to http://www.microsoft.com. After the page is loaded, it hooks the OnMouseOver and OnClick events and then adds text to a list box when the events fire.  Start Visual Studio .NET. Create a new Visual C# .NET Windows Application project. On the COM tab, add a reference to Microsoft.mshtml to the project. In the toolbox, click General. Right-click the open panel, and then click Customize Toolbox. Click to select the Microsoft Web Browser check box, and then click OK.</li> In the toolbox, double-click Explorer.</li> In the toolbox, click Windows Forms, and then double-click ListBox.</li> Arrange the controls so that they are easy to view on the form.</li>  Double-click the form to open the code window for the Form1_Load method. Add the following code for the Form1_Load method: private void Form1_Load(object sender, System.EventArgs e)       { object oURL = &quot;http://www.microsoft.com&quot;; object oEmpty = &quot;&quot;; axWebBrowser1.Navigate2(ref oURL, ref oEmpty, ref oEmpty, ref oEmpty, ref oEmpty); } </li> On the View menu, click Designer, and then click WebBrowser.</li> In the properties, click the Events icon, scroll down to find the DocumentComplete event, type DocumentComplete, and then press ENTER. A code window appears with a template for the DocumentComplete event.</li>  Type or paste the following code in the code window: private void axWebBrowser1_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)       { mshtml.HTMLDocument doc; doc = (mshtml.HTMLDocument)axWebBrowser1.Document; mshtml.HTMLDocumentEvents2_Event iEvent; iEvent = (mshtml.HTMLDocumentEvents2_Event) doc; iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler); iEvent.onmouseover += new mshtml.HTMLDocumentEvents2_onmouseoverEventHandler(MouseOverEventHandler); listBox1.Items.Clear; }                   </li>  Add the following functions to the project in the Form1 class: private bool ClickEventHandler(mshtml.IHTMLEventObj e)       { listBox1.Items.Insert(0, e.type + &quot;:&quot; + e.srcElement.tagName); return true; }       private void MouseOverEventHandler(mshtml.IHTMLEventObj e)        { listBox1.Items.Insert(0, e.type + &quot;:&quot; + e.srcElement.tagName); }                   </li></ol>

back to the top