Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#17

AW: Property eines TObjects setzen Frage

  Alt 13. Mai 2021, 06:04
Ich fürchte bei Font.Color wird es nicht funktionieren, denn das Property ist Font und der hat ein Property Color. Also sollte man nach Property Font suchen und sich die Font-Instanz holen, um dessen Color zu setzen.
Guten Morgen!

Ich bin gerade am aufräumen meines Codes und wollte nun Deinen Vorschlag richtig umsetzen, zur Sicherheit Frage ich nochmal nach ob ich es so anwende wie Du es meintest.

Delphi-Quellcode:
procedure TfrmMain.SetObjectColors(AObject: TObject);
const
  sPropColor = 'Color';
  sPropFont = 'Font';
var
  Context: TRttiContext;
  RType: TRttiType;
  Prop: TRttiProperty;
  Fields: TRttiField;
begin
  Context := TRttiContext.Create;
  RType := Context.GetType(AObject.ClassType);
  for Prop in RType.GetProperties do
  begin
    if Prop.Name = sPropColor then
      Prop.SetValue(AObject, FColorBackground);
    if Prop.Name = sPropFont then
      for Fields in RType.GetFields do
        if Fields.Name = SPropColor then
          Fields.SetValue(AObject, FColorText);
  end;
  Context.Free;
end;
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat