Search code examples
delphifiremonkeycustom-component

Proper way to make custom component containing other components


I want make my own simple component which contains other components. It looks like:

TTag = class(TLayout)
private
    _line: TLine;
    _label: TLabel;
    _background: TRoundRect;
    _button: TLabel;
public
    constructor Create(AOwner: TComponent); override;
end;

When I put this component on form everything is ok and my form structure looks like: enter image description here

but after Delphi IDE reopen it's looks like:

enter image description here

How add sub-components to avoid this strange behavior?


Solution

  • That is a typical case for SetSubComponent:

    Call SetSubComponent to indicate whether this component is a subcomponent. A subcomponent is a component whose Owner is a component other than the form or data module in which it resides. Unless such a component calls SetSubComponent with IsSubComponent set to True, its published properties will not be saved to the form file.

    For each of your sub-components make a call to SetSubComponent(True) in your constructor.