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?
ActiveX is not a programming language, but rather a set of rules for how applications should share information. Programmers can develop ActiveX controls in a variety of languages, including C, C++, Visual Basic, and Java. An ActiveX control is similar to a Java applet.
ActiveX controls are component program objects that Microsoft developed to enable applications to perform specific functions, such as displaying a calendar or playing a video. An ActiveX control is a small program that other applications can reuse to enable the same functionality, without the extra development work.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With