AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Rtti und Enum problem

Ein Thema von KodeZwerg · begonnen am 13. Aug 2022 · letzter Beitrag vom 13. Aug 2022
Antwort Antwort
Benutzerbild von KodeZwerg
KodeZwerg

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

Rtti und Enum problem

  Alt 13. Aug 2022, 10:13
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?
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat
Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
11.009 Beiträge
 
Delphi 12 Athens
 
#2

AW: Rtti und Enum problem

  Alt 13. Aug 2022, 11:22
Delphi-Quellcode:
  SetProperty(Panel1, 'Alignment', TValue.From<TAlignment>(taCenter));
  SetProperty(Panel1, 'BorderStyle', TValue.From<TBorderStyle>(bsSingle));
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat
Benutzerbild von KodeZwerg
KodeZwerg

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

AW: Rtti und Enum problem

  Alt 13. Aug 2022, 12:57
Vielen Dank Uwe, ich teste es heute abend mal!
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat
Benutzerbild von KodeZwerg
KodeZwerg

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

AW: Rtti und Enum problem

  Alt 13. Aug 2022, 21: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
Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
11.009 Beiträge
 
Delphi 12 Athens
 
#5

AW: Rtti und Enum problem

  Alt 13. Aug 2022, 21:59
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
Na ja, du deklarierst in CreateControl deine eigenen Typen für TBorderStyle und 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
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming

Geändert von Uwe Raabe (13. Aug 2022 um 22:02 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von KodeZwerg
KodeZwerg

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

AW: Rtti und Enum problem

  Alt 13. Aug 2022, 22:42
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!
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat
Benutzerbild von KodeZwerg
KodeZwerg

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

AW: Rtti und Enum problem

  Alt 13. Aug 2022, 22:46
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!)
Gruß vom KodeZwerg

Geändert von KodeZwerg (13. Aug 2022 um 22:49 Uhr)
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 00:47 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