Microsoft KB Archive/313891

= HOW TO: Sink Managed C# Events in Internet Explorer Script =

Article ID: 313891

Article Last Modified on 5/17/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Internet Explorer 5.5

-



This article was previously published under Q313891



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

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Runtime.InteropServices

IN THIS TASK
SUMMARY
 * Requirements
 * Steps to Sink Managed Event in Internet Explorer Script
 * Complete Code Listing

REFERENCES



SUMMARY
This step-by-step article shows you how to sink managed events from Component Object Model (COM) clients (unmanaged code) when you write .NET Windows controls. For example, you sink managed events from COM clients when you run script in Internet Explorer.

For information about how to write and to use managed types from COM, refer to the following Microsoft .NET Framework Developer's Guide documentation:

Exposing .NET Framework Components to COM

http://msdn2.microsoft.com/en-us/library/zsfww439(vs.71).aspx

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual Studio .NET
 * Microsoft Internet Explorer (Programming) version 5.5 or later

back to the top

Steps to Sink Managed Event in Internet Explorer Script
 Create a custom Windows Forms control:  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual C# Projects under Project Types, and then click Windows Control Library under Templates.  Define a source interface for the events to be exposed.  Add a GuidAttribute class to the source interface. You must format the string that you pass to the attribute as an acceptable constructor argument for the type Guid. You can use the Guidgen.exe file to create an unused GUID. [GuidAttribute(&quot;0422D916-C11A-474e-947D-45A107038D12&quot;) ] public interface ControlEvents

{       //  Insert code here. }                   </li>  Add an InterfaceType attribute to the source interface to expose COM as an IDispatch interface: [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)] public interface ControlEvents

{       // Insert code here. }                   </li>  Add a DispIdAttribute to any members in the source interface to specify the COM dispatch identifier (DISPID) of a method or a field: public interface ControlEvents {       [DispIdAttribute(0x60020000)] void ClickEvent(int x, int y); }                   </li> Create a new event type to wrap the desired event to expose.</li> Implement the source interface on the custom Windows Forms control.</li>  Add a ComSourceInterfaces attribute to the control to identify the list of interfaces that are exposed as COM event sources: [ClassInterface(ClassInterfaceType.None),ComSourceInterfaces(typeof(ControlEvents))] public class MyWindowControl : System.Windows.Forms.UserControl {

// Insert code here. }                   </li> Compile the control as a dynamic-link library (DLL) file.</li>  Create a script block on the HTML page to hook the event. For example: <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=iso-8859-1' />

<HTML> <HEAD> <TITLE>Sink managed event in Internet Explorer</TITLE> </HEAD> <BODY> <OBJECT id=&quot;ctrl&quot; classid=&quot;YourDllName.dll#ActiveXSourcing.MyWindowControl&quot;> </OBJECT> <SCRIPT LANGUAGE=&quot;JScript&quot;> function ctrl::ClickEvent(a,b) {               alert(&quot;MyWindowControl_ClickEvent&quot;); }       </SCRIPT> </BODY> </HTML> </li> On any client system, use the .NET Framework Configuration tool (Mscorcfg.msc) to grant the assembly the individual permissions that are required.</li></ol>

back to the top

Complete Code Listing
using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; namespace ActiveXSourcing {   public delegate void ClickEventHandler(int x, int y);

//Source interface for events to be exposed. //Add GuidAttribute to the source interface to supply an explicit System.Guid. //Add InterfaceTypeAttribute to indicate that interface is IDispatch interface. [GuidAttribute(&quot;0422D916-C11A-474e-947D-45A107038D12&quot;) ] [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)] public interface ControlEvents

//Add a DisIdAttribute to any members in the source //interface to specify the COM DispId. {       [DispIdAttribute(0x60020000)] void ClickEvent(int x, int y); }

//Add a ComSourceInterfaces attribute to the control to    //identify the list of interfaces that are exposed as COM event sources. [ClassInterface(ClassInterfaceType.None),ComSourceInterfaces(typeof(ControlEvents))] public class MyWindowControl : System.Windows.Forms.UserControl //, ComInteropControlInterface {       System.Windows.Forms.TextBox tx = new TextBox;

private void InitializeComponent {           this.Name = &quot;MyWindowControl&quot;;

}       event ActiveXSourcing.ClickEventHandler ClickEvent; public MyWindowControl : base {

initMyWindowControl;

}       private void initMyWindowControl {

Size = new System.Drawing.Size(300, 50); tx.Text = &quot;Click on the TextBox to invoke 'ClickEvent'&quot;; tx.Size = this.Size; tx.Click += new System.EventHandler(ClickHandler); this.Controls.Add(tx); }       private void ClickHandler(object sender, System.EventArgs e)        { if (ClickEvent != null) {               ClickEvent(0, 0); }       }    } } back to the top

<div class="references_section">