access properties of the class "Host" from grouped properties TPersistent

2

I am creating a custom control that derives from the TEdit class, I have included grouped properties that are in another class of type TPersistent, said properties need access to the properties of the class "host" that derives from the TEDit

type
 TProperties = class(TPersistent)
 private
     FDisabledColor: TColor;
     ...
 procedure SetDisabledColor(const Value: TColor);
 public
 published
   property DisabledColor: TColor read FDisabledColor write SetDisabledColor default clGray;
   ...
 end;


type
 TMiEdit = class(TEdit)
  private
    FProperties : TProperties;

 protected
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Properties: TProperties read FProperties write FProperties;
  end;


procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('MisControles', [TMiEdit]);
end;

{ TProperties }

procedure TProperties.SetDisabledColor(const Value: TColor);
begin
 if Value <> FDisabledColor then
    begin
    if not enabled then //<----aqui quiero acceder a la propiedad ENABLED de mi clase TMiEdit.
       FDisabledColor := Value
    end;

end;




{ TMiEdit }

constructor TMiEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FProperties := TProperties.Create;
  FProperties.FDisabledColor := clGray;
end;

destructor TMiEdit.Destroy;
begin
  FProperties.Free;
  inherited Destroy;
end;



end;

end;

Thanks in advance !!

    
asked by Manuel Paz 28.06.2018 в 16:08
source

1 answer

1

You can define a OwnerEdit property that points to the Owner component of the Properties . In the code that I put you OwnerEdit is type TEdit , but if you are going to use them in other components it can be of type TComponent .

The code could be like this:

type
 TProperties = class(TPersistent)
 private
   FDisabledColor: TColor;
    FEditOwner: TEdit;

   procedure SetDisabledColor(const Value: TColor);
   ///  <summary> Componente Owner de las propiedades </summary>
   property EditOwner:TEdit read FEditOwner write FEditOwner;
 public
 published
   property DisabledColor: TColor read FDisabledColor write SetDisabledColor default clGray;
 end;


type
 TMiEdit = class(TEdit)
  private
    FProperties : TProperties;
 protected
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Properties: TProperties read FProperties write FProperties;
  end;


procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('MisControles', [TMiEdit]);
end;

{ TProperties }

procedure TProperties.SetDisabledColor(const Value: TColor);
begin
  if Value <> FDisabledColor then begin
    if not FEditOwner.Enabled then begin
      FDisabledColor := Value
    end;
  end;
end;

{ TMiEdit }

constructor TMiEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  // Creamos el objeto
  FProperties := TProperties.Create;
  // Asignamos el Owner
  FProperties.EditOwner := Self;

  // Otras props.
  FProperties.FDisabledColor := clGray;
end;

destructor TMiEdit.Destroy;
begin
  FProperties.Free;
  inherited Destroy;
end;

end.
    
answered by 29.06.2018 в 12:54