PDA

Ver la Versión Completa : acceder a propiedades desde propiedades agrupadas Tpersistent


_CALI
28-06-2018, 15:57:59
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

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.


...
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
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 !!


{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;