![]() |
Generische Funktion zum öffen von Forms, ohne Unit Reference
Ein Beispiel wie man Fenster ohne die Form-Unit zu wissen zeigen (Show, ShowModal) kann.
Für was es gut sein kann ? Wir verwenden eine List wo z.B.: DBTabellenName -> FormClassenName gespeichert wird, Dann sagen wir einfach mach mir die EditForm auf die für die Tabelle X zuständig ist und das was, die Formen wissen praktisch selbst was sie zu editieren haben. Deine Form:
Delphi-Quellcode:
Die Routine:
Unit MyForm;
... type TMyForm = ... initialization RegisterClass(TMyForm); // <- Wichtig die Form Classe muss registriert werden !!! end.
Delphi-Quellcode:
Verwendung:
function OpenForm(const AOwner: TComponent; const AFormClassName: string): TForm;
type TFormClass = class of TForm; var FormClass: TFormClass; NewForm : TForm; begin Result := nil; FormClass := TFormClass(GetClass(AFormClassName)); if Assigned(FormClass) then begin NewForm := FormClass.Create(AOwner); Result := NewForm; end; end;
Delphi-Quellcode:
Das funktioniert mit allen Klassen die von TPersistent abstammen.
unit Unit1;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; // <- kein Bezug auf MyForm !!! type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var oTForm: TForm; begin oTForm := OpenForm(nil, 'TForm2'); // <- der erster Parameter ist der Owner der Form z.B.: nil, self // oder Application.MainForm etc. oTForm.Show(); // oder oTForm.ShowMOdal(); etc. end; end. radekj |
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:27 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