Is it wise to create composite controls?

后端 未结 4 1124
渐次进展
渐次进展 2021-01-06 17:31

I have this application that reuses a sort of idiom in a number of places. There\'s a TPanel, and on it are some labels and buttons. The purpose is to allow the user to sele

4条回答
  •  伪装坚强ぢ
    2021-01-06 18:15

    Yes, absolutely it is wise to build components like that because It saves a huge amount of coding.

    Here is a guide to creating them semi-visually: How to Build Aggregate/Composite Components in Delphi

    Essentially, the process outlined in this document is:

    1. Design the layout of your components inside a form in Delphi, placing all the components inside a TPanel (or a descendant thereof).
    2. Select and copy the panel and paste it into a text file.
    3. Replace all instances of " = " with " := ", and add a semi-colon to the end of each line.
    4. Convert all DFM "object" declaration lines to appropriate object constructor code, setting the parent of all visual controls to the container panel.
    5. Clean up any remaining code. Bitmaps will need to be placed in resource files.
    6. Place this new pascal code inside a create constructor for your component. Within the constructor , group object sections under the appropriate sub-component creator.

    Where the document I think is wrong is that, for example, the example component is descended from a TPanel whereas to me it makes more sense to use TCustomPanel and expose only the methods you want to.

    But it also explains how to add OnClick handlers etc.

    the advantage of this method is that you get the layout of the components within the Panel done visually.

提交回复
热议问题