Creating a component with named sub-components?

前端 未结 3 1951
长情又很酷
长情又很酷 2020-11-30 07:17

I need to know the basics behind making a component produce and manage sub-components. I originally tried this by creating a TCollection, and tried to put a nam

3条回答
  •  眼角桃花
    2020-11-30 07:40

    Use the TComponent.SetSubComponent routine:

    type
      TComponent1 = class(TComponent)
      private
        FSubComponent: TComponent;
        procedure SetSubComponent(Value: TComponent);
      public
        constructor Create(AOwner: TComponent); override;
      published
        property SubComponent: TComponent read FSubComponent write SetSubComponent;
      end;
    
    procedure Register;
    
    implementation
    
    procedure Register;
    begin
      RegisterComponents('Samples', [TComponent1]);
    end;
    
    { TComponent1 }
    
    constructor TComponent1.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      FSubComponent := TComponent.Create(Self);  // Nót AOwner as owner here !!
      FSubComponent.Name := 'MyName';
      FSubComponent.SetSubComponent(True);
    end;
    
    procedure TComponent1.SetSubComponent(Value: TComponent);
    begin
      FSubComponent.Assign(Value);
    end;
    

    I now understand this sub component would be part of a collection item. In that case: no difference, use this method.

提交回复
热议问题