Mega Code Archive

 
Categories / Delphi / Forms
 

Show a TForm with its Classname

Title: show a TForm with its Classname? { M chten Sie z.B einen Formular mit einer Prozedure initialisieren bzw. anzeigen ohne Type-informationen oder classreferenzen bergeben zu m ssen? Dies geht sowohl im Programmcode als auch zur Laufzeit. Dabei gilt z.B.: bei Formulare werden die von Delphi angelegten globalen Variablen bzw. die automatische Erzeugung im Hauptprogramm berfl ssig: Die Anbindung der Units und die RegisterClasses(...) sind notwendig!!! } { You may wish to initialise some descendant of a given class (runtime / designtime) using a simple procedure but without passing Type information of a specified class to it and thus stay flexible ? You can deal with the global variable of your forms as you wish, you'll never need them again... You just need to bind your form units and to register your classes (Delphi won't, if you delete the global vars). } uses MyFormOne, MyFormTwo; procedure ShowOneOfMyForm(FormClassName: string); begin with TFormClass(FindClass(FormClassName)).Create(Application) do try ShowModal; finally Free; end; end; { Geben Sie z.B. "TMyFormTwo" in dem TEdit und clicken Sie auf dem Knopf } { How to use it? Give "TMyFormTwo" in a TEdit and click the TButton...} procedure TForm1.btShowMyFormClick(Sender: TObject); begin //at runtime ShowOneOfMyForm(InputEdit.Text); // or directly in your code ShowOneOfMyForm('TMyFormOneF'); end; initialization RegisterClasses([TMyFormOneF, TMyFormTwoF]); end.