Microsoft KB Archive/316516

= How To Sink Managed Visual Basic .NET Events in Internet Explorer Script =

Article ID: 316516

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Internet Explorer 5.5

-



This article was previously published under Q316516



For a Microsoft Visual C# .NET version of this article, see 313891.

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

REFERENCES



SUMMARY
This article describes 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 Microsoft Internet Explorer. For information about how to write and how to use managed types from COM, refer to the following Microsoft .NET Framework Developer's Guide documentation:

Exposing .NET Framework Components to COM

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 (Programming) version 5.5 or 6

back to the top

To Sink a Managed Event in an Internet Explorer Script
 Create a custom Windows Forms control:  Start Visual Studio .NET. On the File menu, point to New, and then click Project. In the Add New Project dialog box, click Visual Basic Projects under Project Types, and then click Windows Control Library under Templates. Name the project MyVBControl.  Define a source interface for the events that are to be exposed.</li>  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 Guid type. You can use Guidgen.exe to create an unused GUID. <GuidAttribute(&quot;1F98211C-7A71-4588-8D4A-AD85CA80BAE7&quot;), _ Public Interface ControlEvents Sub ClickEvent(ByVal x As Integer, ByVal y As Integer)<BR/>

End Interface </li>  Add an InterfaceType attribute to the source interface to expose COM as an IDispatch interface: InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)> _ Public Interface ControlEvents Sub ClickEvent(ByVal x As Integer, ByVal y As Integer)<BR/>

End Interface </li>  Add DispIdAttribute to any members in the source interface to specify the COM dispatch identifier (DISPID) of a method or a field: Public Interface ControlEvents 'Add DisIdAttribute to any members in the source interface to specify the COM DISPID. <DispIdAttribute(1)> _ Sub ClickEvent(ByVal x As Integer, ByVal y As Integer) End Interface </li> Create a new event type to wrap the event that is to be exposed.</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: <ComSourceInterfaces(GetType(ControlEvents)), _ ClassInterface(ClassInterfaceType.None)> _ Public Class MyVBControl </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;MyVBControl.dll#MyVBControl.MyVBControl&quot;> </OBJECT> <SCRIPT LANGUAGE=&quot;JScript&quot;> function ctrl::ClickEvent(a,b) {               alert(&quot;MyWindowControl_ClickEvent&quot;); }         </SCRIPT> </BODY> </HTML> </li> On any client computer, use the .NET Framework Configuration tool (Mscorcfg.msc) to grant the assembly the individual permissions that are required. Or, follow these steps to reduce the framework security for testing:  Start a command prompt in Visual Studio .NET.</li> At the command prompt, turn the framework security off by using the caspol -s off command.</li> On the Internet Explorer client, reduce the local internet security to low for testing. On the Tools menu in Internet Explorer, click Internet Options, and then click the Security tab.</li></ol> </li></ol>

Note You must place the SCRIPT block in the HTML page after the OBJECT tag for the function to work. This requirement is caused by the loading order.

back to the top

Complete Sample Code Listing
Imports System Imports System.ComponentModel Imports System.Drawing Imports System.Windows.Forms Imports System.Runtime.InteropServices Public Delegate Sub ClickEventHandler(ByVal x As Integer, ByVal y As Integer)

' Source interface for events to be exposed. ' Add GuidAttribute to the source interface to supply an explicit System.Guid. ' Add InterfaceTypeAttribute to indicate that the interface is an IDispatch interface.

<GuidAttribute(&quot;1F98211C-7A71-4588-8D4A-AD85CA80BAE7&quot;), _ InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)> _ Public Interface ControlEvents ' Add DisIdAttribute to any members in the source interface to specify the COM DISPID. <DispIdAttribute(1)> _ Sub ClickEvent(ByVal x As Integer, ByVal y As Integer) End Interface

' Add a ComSourceInterfaces attribute to the control to identify the list of interfaces that are exposed as COM event sources.

<ComSourceInterfaces(GetType(ControlEvents)), _ ClassInterface(ClassInterfaceType.None)> _ Public Class MyVBControl Inherits System.Windows.Forms.UserControl Dim tx As New System.Windows.Forms.TextBox Public Event ClickEvent(ByVal x As Integer, ByVal y As Integer)


 * 1) Region &quot; Windows Form Designer generated code &quot;

Public Sub New MyBase.New

'This call is required by the Windows Form Designer. InitializeComponent Me.Name = &quot;MyVBControl&quot; initMyVBWindowControl

'Add any initialization after the InitializeComponent call.

End Sub

' UserControl overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose End If       End If        MyBase.Dispose(disposing) End Sub

' Required by the Windows Form Designer. Private components As System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Windows Form ' Designer. You can modify it by using the Windows Form Designer. ' Do not modify it by using the code editor. <System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent '       ' MyVBControl '

End Sub Private Sub initMyVBWindowControl

Size = New System.Drawing.Size(300, 50) tx.Text = &quot;Click on the TextBox to invoke 'ClickEvent'&quot; tx.Size = Me.Size

AddHandler tx.Click, New System.EventHandler(AddressOf ClickHandler)

Me.Controls.Add(tx) End Sub Private Sub ClickHandler(ByVal sender As Object, ByVal e As System.EventArgs) RaiseEvent ClickEvent(0, 0)

End Sub


 * 1) End Region

End Class back to the top

<div class="references_section">