views:

638

answers:

1

I'm seeing a few questions related to this on SO already, but I think mine is sufficiently different to not be considered a duplicate (if I'm wrong let me know).

I have an ActiveX control I've written in C# and while I have it mostly working, I want to raise an event in JavaScript when it's clicked (it displays an image so it's a visual element on the page).

The end goal of what I'm looking to accomplish is no different than if it were a <span> tag and it had an onclick event to raise a JavaScript function when the area of the tag were clicked.

Most of the stuff I've read on it goes into very fine detail on how to handle events in an ActiveX control and send info back/forth, and that's fine, but it seems overly complicated. I'm not looking to communicate with the ActiveX control, I just need a JavaScript function to fire off when I click it, in a way similar to a <span> or <div> tag. I can handle everything else in JavaScript. Simply wrapping the control in a <span> or <div> with an onclick event has no effect - the ActiveX control pretty much overrides it.

Is there a simple way to handle this for an ActiveX control written in C#?

I guess another way of putting it is - I'm working with a third party control and we have to use code similar to the following to get it to communicate with our HTML page via JavaScript

<script type="text/javascript" event="OnMouseClick(index)" for="AXObjectName"> 
        <!--
         AXObjectName_OnMouseClick(index);
        //-->
</script>

Where AXObjectName is the name/id of the control and AXObjectName_OnMouseClick is the name of the JavaScript function it will fire in my code, passing an index parameter. However, what all do I have to do to set up a method like OnMouseClick in the control? And if I don't want to pass any actual information (i.e., no index) do I even have to go this far?

+3  A: 

ActiveX events are handled via COM, so you need to delve in there a bit unfortunately.

The thing to remember with COM is that everything is handled via interfaces, so you normally need to create two interfaces, one for any properties and one for your events.

The key for events is marking your class with the ComSourceInterfaces attribute, which is described by MSDN as "Identifies a list of interfaces that are exposed as COM event sources for the attributed class."

This simple class structure should work for you (it has for me in the past).

namespace MyActiveX
{
    [Guid("Your-GUID") ,InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IActiveXEvents
    {
        [DispId(1)]
        void OnMouseClick(int index);
    }

    [Guid("Another-GUID"),InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IActiveX
    {       
        //[DispId(1)]
        // Any properties you want here like this
        // string aProperty { get; set; }        
    }

    [ComVisible(true)]
    [Guid("Yet-Another-GUID"), ClassInterface(ClassInterfaceType.None)]
    [ProgId("MyActiveX")]
    [ComSourceInterfaces(typeof(IActiveXEvents))]
    public partial class MyActiveX : UserControl, IActiveX
    {
        public delegate void OnMouseClickHandler(int index);

        public event OnMouseClickHandler OnMouseClick;

        // Dummy Method to use when firing the event
        private void MyActiveX_nMouseClick(int index)
        {

        }

        public MyActiveX()
        {
            InitializeComponent();

            // Bind event
            this.OnMouseClick = new OnMouseClickHandler(this.MyActiveX_MouseClick)
        }

        public void FireTheEvent()
        {
            int index = -1;
            this.OnMouseClick(index);
        }       
    }
}

If you don't need any properties you can just exclude the IActiveX interface. Also if you are going to use the Click event, you will need to mark it as new to pass it to COM.

ParmesanCodice
Literally moments ago I got it working using the things I learned here: http://blog.ianchivers.com/wordpress/?p=22. Your code looks about like what I wound up with (and the link I just posted also includes javascript) so you win.
Schnapple
Haha :) That's a really good blog post on this though, *good* info on ActiveX and C# is pretty hard to come by. I still have found out how to do this: http://stackoverflow.com/questions/746822/accessing-activex-ambient-properties-from-c
ParmesanCodice
AAAAAAHHHH!!! My eyes! Why can't this be easy?
MusiGenesis
@MusiGenesis I've come to believe that is part of a sinister Microsoft plan to kill off ActiveX. Doesn't help us guys who still need to produce this technology. Things are a *bit* easier in VB .NET see http://www.codeproject.com/KB/COM/VS2005ComDllWalkThru.aspx
ParmesanCodice