How to create a component property that lists other components?

风流意气都作罢 提交于 2019-12-02 02:25:40

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/20159744/how-to-create-a-component-property-that-lists-other-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!