views:

1061

answers:

2

I have an ActiveX control (an OCX file) which raises an event. I want to catch that event in C#. How do I go about doing it?

I can catch the control's event in JavaScript, here is the code for that

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>
<body >
<script type="text/javascript" for="CRMCntrl1" event="NewCall(szCallID, szCallType, nCallStartTime, szCLI, szOtherInfo)">    
     document.getElementById("abc").innerHTML="CallID: " + szCallID + "</br>" +
      "CallType: " + szCallType + "</br>" + 
      "CallStartTime: " + nCallStartTime + "</br>" +
      "CLI: " + szCLI + "</br>" + 
      "OtherInfo: " + szOtherInfo + "</br>" ;

</script>
<p>
<object id="CRMCntrl1" classid="clsid:D26FE0DF-5CAC-44E4-AA7A-E1794D9634D1">
</object>
</p>
<div id="abc">
</div>
</body>
</html>

I want to do it in C#. So I added a COM reference of the control. It contains interfaces which I implemented in my Form's class. In one of the interface there is an event, I subscribed to that event, basically it is the event which I want to capture, but it isn't raised.

Here is my C# Code

// other namespaces here
using CRMCNTRLLib;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form, CRMCntrl
    {
     public Form1()
     {
      InitializeComponent();

      this.NewCall += new _DCRMCntrlEvents_NewCallEventHandler(this.OnNewCall);
     }

     public event _DCRMCntrlEvents_NewCallEventHandler NewCall;

     public void AboutBox()
     {
      MessageBox.Show("steadfast");
     }

     public void OnNewCall(string szCallID, string szCallType, int nCallStartTime, string szCLI, string szOtherInfo)
     {
      MessageBox.Show(szCallID + szCallType + nCallStartTime + szCLI + szOtherInfo);
     }
    }
}
+1  A: 

What you have posted should work, except... something looks funny about what you are doing with the definition of the form itself. You are deriving the class Form1 from Form and CRMCntrl? This won't create an instance of the control in the way you seem to be expecting. Try putting a new instance of the ActiveX control on the form instead, and see what happens.

Jon Grant
Hey, thank ye very much :P
hab
+2  A: 

From your code it seems like CRMCntrl is an interface with a single NewCall event. So if you want to implement that interface, then you are the one who is responsible for firing the event, not handling it.

To handle an event fired from your ActiveX control, you need to use the instance of your actual control - that is where the event should come from. If you added the control using the designer, try to find what is the name of the private field which contains a reference to your control.

Somehow you will need to change this:

this.NewCall +=
       new _DCRMCntrlEvents_NewCallEventHandler(this.OnNewCall);

to this:

myActiveXControl.NewCall +=
       new _DCRMCntrlEvents_NewCallEventHandler(this.OnNewCall);

where myActiveXControl is the name of your control (which also implements the interface providing the event).

Groo
Thanks, this worked :)
hab