Question

I have a 3rd party Active X .ocx file that I have imported into Delphi XE2 and created a TLB file for.

The Active X library is a 'non-visual' component but shows as a image if dropped onto a form.

I want to call this component in an Active X library and can access its methods and properties fine, but do not know how to access its events. I thought I could perhaps add it to a Data Module but this doesn't seem to be available as an option in the Tool Palette. Should this be possible?

I tried adding the events by doing something like this: actX3Party := T3Party.Create(nil); actX3Party.On3PartyEvent := myEventHandler;

but don't know how to make 'myEventHandler' an Event Handler as there is no form.

Thank you

Was it helpful?

Solution

You don't need a form to implement an event handler. All you need is a class. You can implement an event handler using either instance methods or class methods. Naturally, if you choose to use instance methods, then you need to instantiate an instance.

So, decide whether you want to use instance methods or class methods. Then create a class that defines your event handlers. If you are using instance methods, instantiate the class. Finally, assign your handlers to the events.

For example:

type
  TMyClass = class
  public
    class procedure MyHandler1(Sender: TObject);
    procedure MyHandler2(Sender: TObject);
  end;

I don't know what parameters you event receives, so the above is just for example's sake. And obviously you'll need to implement the methods.

You can use the class procedure immediately:

actX3Party.On3PartyEvent := TMyClass.MyHandler1;

For the instance method, make an instance:

myInstance := TMyClass.Create;
actX3Party.On3PartyEvent := myInstance.MyHandler2;

If the code which is assigning the event handlers to the ActiveX control exists in a class, then you don't need another class. You can implement your handlers right there in that class.

So, one more time, event handlers do not need to be implemented in a form. They are simply methods of a class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top