Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   acceder a propiedades desde propiedades agrupadas Tpersistent (https://www.clubdelphi.com/foros/showthread.php?t=93247)

_CALI 28-06-2018 15:57:59

acceder a propiedades desde propiedades agrupadas Tpersistent
 
Buenas, estoy creando un control personalizado que deriva de la clase TEdit, he incluido propiedades agrupadas que estan en otra
clase del tipo TPersistent, dichas propiedades necesitan acceder a las propiedades de la clase "host" que deriva del TEDit

Código Delphi [-]
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;

Gracias de Antemano!!

ecfisa 28-06-2018 19:08:51

Hola.
Cita:

Empezado por _CALI (Mensaje 527374)
Código Delphi [-]
...
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;
...

Es que la clase TProperties no hereda de TMiEdit ni compone ninguna instancia de ella. No está enterada de la existencia de la clase TMiEdit y por tanto no puede hacer referencia a ningún método o atributo de ella.

Saludos :)

_CALI 29-06-2018 14:44:52

Cita:

Empezado por ecfisa (Mensaje 527377)
Hola.

Es que la clase TProperties no hereda de TMiEdit ni compone ninguna instancia de ella. No está enterada de la existencia de la clase TMiEdit y por tanto no puede hacer referencia a ningún método o atributo de ella.

Saludos :)

Gracias por responder ecfisa, mas o menos suponía lo que indicas, la mejor opción solución que busque por la web fue crear un evento en la clase TPersistent donde notifica los cambios
de las propiedades de TPeristent a la clase contenedora ( TMiEdit ) para hacer las acciones correspondientes. Funciona bien!

Muchas Gracias !!

Código Delphi [-]
{TProperties}
TProperties = class(TPersistent)
  private
    FDisabledColor: TColor;
    ...

    FOnChange: TNotifyEvent;
    procedure Changed;
    procedure SetDisabledColor(Value: TColor);
   
   ...
end;

procedure TProperties.Changed;
begin
  if Assigned(FOnChange) then
    FOnChange(Self);
end;

procedure TProperties.SetDisabledColor(Value: TColor);
begin
  if FDisabledColor <> Value then
  begin
    FDisabledColor := Value;
    Changed; <<---notifico a la clase contenedora que se ha cambiado el color
  end;
end;



{TMiEdit}

constructor TMiEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FProperties := TProperties.Create;
  FProperties.OnChange := PropertiesChanged; <<---aqui se asigna el procedimiento local  al evento de Tpersistent
  ...
end;

procedure TMiEdit.PropertiesChanged(Sender: TObject);
begin
  if Enabled then <<---aqui por fin puedo hacer referencia a mi mismo objeto
  begin
    inherited Color := Color;
    Font.Color := FProperties.EnabledTextColor;
  end
  else
  begin
    inherited Color := FProperties.DisabledColor;
    Font.Color := FProperties.DisabledTextColor;
  end;
end;


La franja horaria es GMT +2. Ahora son las 01:08:15.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi