Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi Rtti und Enum problem (https://www.delphipraxis.net/211203-rtti-und-enum-problem.html)

KodeZwerg 13. Aug 2022 10:13

Rtti und Enum problem
 
Hallo liebe Delphi Freunde, in einem kleinen Projekt von mir kann ein Anwender "unbekannte" wincontrols an meine Klasse übergeben, innerhalb meiner Klasse setze ich auch Properties des unbekannten Controls.
Dabei bin ich so vorgegangen das meine Basis-Klasse für die unbekannten dinger vom TControl abgeleitet werden.
(ich habe unter Lazarus angefangen und dort funktioniert es, wenn ich es unter Delphi ausführe erhalte ich einen Typecast Error)
Delphi-Quellcode:
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 := (LRttiProperty.GetValue(Ctrl) = AValue); // Error: Operator is not overloaded: "TValue" = "TValue"
    Result := True;
  end;
end;
mit dieser Methode setze ich Felder die TControl/TWinControl nicht bereitstellt, funktioniert soweit auch ganz gut.
Delphi-Quellcode:
      {$IFDEF FPC}
      SetProperty(FControls[ii], 'Alignment', Ord(taCenter));
      SetProperty(FControls[ii], 'BorderStyle', Ord(bsSingle));
      {$ELSE FPC}
      SetProperty(FControls[ii], 'Alignment', 2); // typecast error
      SetProperty(FControls[ii], 'BorderStyle', 1); // typecast error
      {$ENDIF FPC}
Wie macht man es unter Delphi korrekt bitte?

Uwe Raabe 13. Aug 2022 11:22

AW: Rtti und Enum problem
 
Delphi-Quellcode:
  SetProperty(Panel1, 'Alignment', TValue.From<TAlignment>(taCenter));
  SetProperty(Panel1, 'BorderStyle', TValue.From<TBorderStyle>(bsSingle));

KodeZwerg 13. Aug 2022 12:57

AW: Rtti und Enum problem
 
Vielen Dank Uwe, ich teste es heute abend mal!

KodeZwerg 13. Aug 2022 21:15

AW: Rtti und Enum problem
 
Zitat:

Zitat von Uwe Raabe (Beitrag 1510082)
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 :-D

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.

Uwe Raabe 13. Aug 2022 21:59

AW: Rtti und Enum problem
 
Zitat:

Zitat von KodeZwerg (Beitrag 1510103)
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 :-D

Na ja, du deklarierst in CreateControl deine eigenen Typen für
Delphi-Quellcode:
TBorderStyle
und
Delphi-Quellcode:
TAlignment
. Die sind aber nicht zuweisungskompatibel zu denen in System.Classes und Vcl.Forms - auch wenn sie identisch deklariert werden.

Edit: Das ist so als ob du schreibst:
Delphi-Quellcode:
    TEdit(LControl).Alignment := taCenter;
//    SetProperty(LControl, 'Alignment', TValue.From<TAlignment>(taCenter)); // typecast error

KodeZwerg 13. Aug 2022 22:42

AW: Rtti und Enum problem
 
Ja ich wollte eigentlich nicht die Forms mit aufnehmen aber nun mache ich es und es läuft wie gewünscht, der Fehler saß eindeutig vor dem Bildschirm, vielen Dank für die Erklärung!

KodeZwerg 13. Aug 2022 22:46

AW: Rtti und Enum problem
 
Gibt es denn unter Delphi evtl. eine möglich das Verhalten wie bei FreePascal nachzuäffen indem ich eine Zahl für den Enum-Index (also ohne type definition) eingebe?

(Ps: Ich bin mehr als froh das nun erstmal überhaupt das macht was es soll, tausend Dank dafür!)


Alle Zeitangaben in WEZ +1. Es ist jetzt 16:59 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz