Search code examples
delphiactivex

How expose the properties of a component created in an ActiveX form?


Can you publish the properties of a control that is inside an ActiveX form?

Example: I have a form with an TADOConnection component. I wish that the properties of this component can be modified by the user when he loads my ActiveX control.

alt text

UPDATE

@TOndrej gives me a very nice sample, but this sample only works for components derived from an ActiveX control. How can I accomplish this same effect with a VCL component like a TImage or TMemo? Is it possible to publish all the properties without rewriting each property to expose them manually?


Solution

  • ADO components are already ActiveX objects, so the easiest way is to expose the connection as a simple property of your ActiveX form:

    In the type library editor, add "Microsoft ActiveX Data Objects 2.1 Library" to the list of used libraries. This will generate ADODB_TLB.pas unit in your project directory.

    alt text

    Then you can declare a new read-only property Connection of type Connection (this type is declared in ADODB_TLB unit) in your IActiveFormX interface.

    alt text

    In the implementation, you can simply return the interface from your TADOConnection component:

    type
      THackADOConnection = class(TADOConnection);
    
    function TActiveFormX.Get_Connection: Connection;
    begin
      Result := Connection(THackADOConnection(ADOConnection).ConnectionObject);
    end;
    

    The THackADOConnection typecast is only necessary because ConnectionObject is protected. The outer Connection typecast is there to get rid of the compiler error "Incompatible types: ADODB_TLB._Connection and ADOInt._Connection."