Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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:

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

To Sink a Managed Event in an Internet Explorer Script

  1. Create a custom Windows Forms control:
    1. Start Visual Studio .NET.
    2. On the File menu, point to New, and then click Project.
    3. In the Add New Project dialog box, click Visual Basic Projects under Project Types, and then click Windows Control Library under Templates.
    4. Name the project MyVBControl.
  2. Define a source interface for the events that are to be exposed.
  3. 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("1F98211C-7A71-4588-8D4A-AD85CA80BAE7"), _
    Public Interface ControlEvents
        
        Sub ClickEvent(ByVal x As Integer, ByVal y As Integer)<BR/>
    
    End Interface
    					
  4. 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
    					
  5. 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
    					
  6. Create a new event type to wrap the event that is to be exposed.
  7. Implement the source interface on the custom Windows Forms control.
  8. 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
    					
  9. Compile the control as a dynamic-link library (DLL) file.
  10. Create a script block on the HTML page to hook the event. For example:
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <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="ctrl"classid="MyVBControl.dll#MyVBControl.MyVBControl">
              </OBJECT>
              <SCRIPT LANGUAGE="JScript">
                function ctrl::ClickEvent(a,b)
                {
                    alert("MyWindowControl_ClickEvent");
                }
              </SCRIPT>
         </BODY>
    </HTML>
    					
  11. 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:
    1. Start a command prompt in Visual Studio .NET.
    2. At the command prompt, turn the framework security off by using the caspol -s off command.
    3. 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.
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.

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("1F98211C-7A71-4588-8D4A-AD85CA80BAE7"), _
    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)

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()
        Me.Name = "MyVBControl"
        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 = "Click on the TextBox to invoke  'ClickEvent'"
        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


#End Region



End Class
				

↑ Back to the top


References

For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
313891 How To Sink Managed C# Events in Internet Explorer
316510 PRB: Security Exception When You Use Event Handlers in Internet Explorer
For more information, visit the following Microsoft Web Developer Network (MSDN) Web sites:

↑ Back to the top


Keywords: KB316516, kbhowtomaster

↑ Back to the top

Article Info
Article ID : 316516
Revision : 4
Created on : 7/1/2004
Published on : 7/1/2004
Exists online : False
Views : 274