SUMMARY
This article describes how to catch document events for the
WebBrowser control in Microsoft Visual C# .NET.
back to the topRequirements
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 topDescription 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 topCreate 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.
- In the toolbox, double-click Explorer.
- In the toolbox, click Windows Forms, and then double-click ListBox.
- Arrange the controls so that they are easy to view on the form.
- 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);
}
- On the View menu, click Designer, and then click WebBrowser.
- 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.
- 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();
}
- 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 topNotes
- 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 topREFERENCES
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