delphipropertieslazarussynedit

How to create a component property that lists other components?


The SynEdit component has the property "Highlighter", which contains a dropdown-list in which all the currently existant Highlighters are listed (design-time). To me this seems like a very important concept for design-time components, but I'm simply unable to find out how it works:

Let's assume you drop down a TSynEdit and a TSynPasSyn onto your form. Then you click the TSynedit which has the property Highlighter. You are now able to select the previously created TSynPasSyn. If you create another TSynPasSyn, it will be added to this list too. My question:

Which is the best way to do such a thing in your own component? Can you simply use a property editor or do you need custom helper-classes, or something completely different?


Solution

  • Maybe this will surprise you, but nothing is required to get existing components listed in the property editor of a component property in your own component. Just declare the property as the desired type, and the VCL framework will do the rest.

    For example, consider this very simple component:

    type
      TButtonSelector = class(TComponent)
      private
        FButton: TButton;
      published
        property Button: TButton read FButton write FButton;
      end;
    

    After installing this component in the IDE, when you select the Button property in the Object Inspector, all existing buttons on the current Form are listed.

    This is all build in in DesignEditors.TComponentProperty, which means that the only requirement is to let the object you want to select descent from TComponent.