Thema: Delphi Rtti und Enum problem

Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.691 Beiträge
 
Delphi 11 Alexandria
 
#4

AW: Rtti und Enum problem

  Alt 13. Aug 2022, 22:15
Delphi-Quellcode:
  SetProperty(Panel1, 'Alignment', TValue.From<TAlignment>(taCenter));
  SetProperty(Panel1, 'BorderStyle', TValue.From<TBorderStyle>(bsSingle));
Vielen Dank aber das Resultat bleibt gerade das gleich, es mag an mir liegen deshalb zeige ich etwas ausführlicher wie ich es falsch mach

Delphi-Quellcode:
unit Unit26;

interface

uses
  System.TypInfo, System.Rtti,
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm26 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form26: TForm26;

implementation

{$R *.dfm}


function SetProperty(const AControl: TControl; const AProperty: string; const AValue: TValue): Boolean;
var
  LControl: TControl;
  LRttiContext: TRttiContext;
  LRttiProperty: TRttiProperty;
begin
  Result := False;
  LControl := AControl;
  LRttiProperty := LRttiContext.GetType(LControl.ClassType).GetProperty(AProperty);
  if ((LRttiProperty <> nil) and (LRttiProperty.Visibility in [mvPrivate, mvProtected, mvPublic, mvPublished])) then
  begin
    LRttiProperty.SetValue(LControl, AValue);
    Result := True;
  end;
end;

function CreateControl(const AOwner: TComponent; const AParent: TWinControl; AClass: TControlClass): Boolean;
type
  TFormBorderStyle = (bsNone, bsSingle, bsSizeable, bsDialog, bsToolWindow, bsSizeToolWin);
  TBorderStyle = bsNone..bsSingle;
  TAlignment = (taLeftJustify, taRightJustify, taCenter);
var
  i, ii: Integer;
  LControl: TControl;
begin
  Result := False;
  LControl := AClass.Create(AOwner);
  try
    LControl.Parent := AParent;
    LControl.Visible := False;
    LControl.Enabled := False;
    SetProperty(LControl, 'ReadOnly', True);
    LControl.Width := 200;
    LControl.Left := High(Integer);
    SetProperty(LControl, 'Alignment', TValue.From<TAlignment>(taCenter)); // typecast error
    SetProperty(LControl, 'BorderStyle', TValue.From<TBorderStyle>(bsSingle)); // typecast error
    LControl.Align := alLeft;
    SetProperty(LControl, 'MaxLength', 1);
    SetProperty(LControl, 'Color', clWhite);
    SetProperty(LControl, 'Text', 'X');
    SetProperty(LControl, 'Caption', 'Y');
  finally
    Result := True;
  end;
end;


procedure TForm26.Button1Click(Sender: TObject);
begin
  CreateControl(Form26, Form26, TEdit);
end;

end.
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat