![]() |
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:
mit dieser Methode setze ich Felder die TControl/TWinControl nicht bereitstellt, funktioniert soweit auch ganz gut.
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;
Delphi-Quellcode:
Wie macht man es unter Delphi korrekt bitte?
{$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} |
AW: Rtti und Enum problem
Delphi-Quellcode:
SetProperty(Panel1, 'Alignment', TValue.From<TAlignment>(taCenter));
SetProperty(Panel1, 'BorderStyle', TValue.From<TBorderStyle>(bsSingle)); |
AW: Rtti und Enum problem
Vielen Dank Uwe, ich teste es heute abend mal!
|
AW: Rtti und Enum problem
Zitat:
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. |
AW: Rtti und Enum problem
Zitat:
Delphi-Quellcode:
und
TBorderStyle
Delphi-Quellcode:
. Die sind aber nicht zuweisungskompatibel zu denen in System.Classes und Vcl.Forms - auch wenn sie identisch deklariert werden.
TAlignment
Edit: Das ist so als ob du schreibst:
Delphi-Quellcode:
TEdit(LControl).Alignment := taCenter;
// SetProperty(LControl, 'Alignment', TValue.From<TAlignment>(taCenter)); // typecast error |
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!
|
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 04:28 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz