How to handle document events in a Visual C# .NET application (312777)



The information in this article applies to:

  • Microsoft Visual C# .NET (2002)

This article was previously published under Q312777
For a Microsoft Visual Basic .NET version of this article, see 311284.

IN THIS TASK

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.
  1. Start Visual Studio .NET.
  2. Create a new Visual C# .NET Windows Application project.
  3. On the COM tab, add a reference to Microsoft.mshtml to the project.
  4. In the toolbox, click General.
  5. Right-click the open panel, and then click Customize Toolbox.
  6. Click to select the Microsoft Web Browser check box, and then click OK.
  7. In the toolbox, double-click Explorer.
  8. In the toolbox, click Windows Forms, and then double-click ListBox.
  9. Arrange the controls so that they are easy to view on the form.
  10. 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 = "http://www.microsoft.com";
    			object oEmpty = "";
    			axWebBrowser1.Navigate2(ref oURL, ref oEmpty, ref oEmpty, ref oEmpty, ref oEmpty);
    		}
  11. On the View menu, click Designer, and then click WebBrowser.
  12. 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.
  13. 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();
    		}
    					
  14. Add the following functions to the project in the Form1 class:
    		private bool ClickEventHandler(mshtml.IHTMLEventObj e)
    		{
    			listBox1.Items.Insert(0, e.type + ":" + e.srcElement.tagName);
    			return true;
    		}
    		private void MouseOverEventHandler(mshtml.IHTMLEventObj e)
    		{
    			listBox1.Items.Insert(0, e.type + ":" + e.srcElement.tagName);
    		}
    					
back to the top

Notes

  • If you are automating Internet Explorer, the process is the same. Instead of using axWebBrowser1, you use your local variable name for Internet Explorer.
  • This sample does not account for framesets. When you open a frameset, you may not see any events in your application. In this case, you must add code to handle the chance of framesets.
back to the top

REFERENCES

For more information about the WebBrowser control and the methods, properties, and events that it exposes, visit the following Microsoft Developer Network (MSDN) Web site: For more information about how to handle events in the Microsoft .NET Framework, visit the following MSDN Web site: For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:back to the top

Modification Type:MajorLast Reviewed:4/21/2006
Keywords:kbControl kbEvent kbProgramming kbHOWTOmaster KB312777 kbAudDeveloper