New Tip - ...create/manage any forms only through the Classname ?

  • From: "SwissDelphiCenter.ch" <newsletter@xxxxxxxxxxxxxxxxxxxx>
  • To: sdcnewtip@xxxxxxxxxxxxx
  • Date: Sun, 10 Mar 2002 08:55:03 +0100

* SwissDelphiCenter new Tip newsletter
* www.swissdelphicenter.ch
*
* To unsubscribe send a mail to sdcnewtip-request@xxxxxxxxxxxxx with the
* subject unsubscribe


Hallo,

Folgender neuer Tip ist neu auf SwissDelphiCenter verfügbar:

Besuchen Sie die Programmier Tips unter 
http://www.swissdelphicenter.ch/de/tipsindex.php


Bewerten Sie diesen Tip nach Schwierigkeitsgrad, Nützlichkeit und Gesamthaft auf
http://www.swissdelphicenter.ch/de/showcode.php?id=1031



Autor:
Loïs Bégué <delphi@xxxxxxxx>
www.begue.de


-------------
...beliebige Formulare nur anhand der Klasse erzeugen und verwalten ?
-------------
Kategorie: VCL



{
  Wenn Sie verschiedene Formular zentral initialisieren möchten
  und dies mittels der Klassennamen erledigen möchten, dann
  genügt folgendes...
  Dabei können Sie auch allgemeine Aufgaben dazu erledigen...
}

{
  If you'd like to zentralize Form creation only using class reference
  information, then use the following...
  You should do some generic jobs by the way, as shown in the example.
}

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

{ Main Form  declaration }
type
  TForm2 = class(TForm)
    btShowOneForm: TButton;
    procedure btShowOneFormClick(Sender: TObject);
  private
  public
  end;

var
  Form2: TForm2;


  { extra stuff for the example...}

{
  First, I declare some generic stuff to be available from all
  of my automatic created forms: interface is the best
}
type
  IMyGenericFormInterface = interface(IUnknown)
    ['{FDABF2E0-2279-11D6-9648-000374890932}']
    function GetTitel: string;
    property Titel: string read GetTitel;
  end;
{
  Then I can inflate the functionality of any of my declared
  Form classes, adding a ref to my generic interface.
  Notice: the function must be implemented in all the children
  classes using he interface, but the main program doesn't care
  what is there being done...just use it.
}

type
  TMyFormTwo = class(TForm2, IMyGenericFormInterface)
  private
    function GetTitel: string;
  end;
{
  You have to register your form class using RegisterClasses
  or to declare a variable of the given class so Delphi'll
  do it for you...
}
var
  MF2: TMyFormTwo;
  {...END of extra stuff }

implementation

{$R *.dfm}


function TMyFormTwo.GetTitel: string;
begin
  Result := 'Titel of 2. Form';
end;

{ !!! THIS is the "Global creation procedure" you should use  !!! }
procedure ShowClassifiedForm(TheFormClass: TFormClass);
var
  X: TForm;
begin
  X := TheFormClass.Create(Application);
  try
    // Extra stuff: Check to see if TheFormClass
    //              can manage with Title
    if Supports(X, IMyGenericFormInterface) then
      ShowMessage((X as IMyGenericFormInterface).Titel);

    X.ShowModal;
  finally
    X.Free;
  end;
end;

procedure TForm2.btShowOneFormClick(Sender: TObject);
begin
  //This will modal show a new copy a the Form2...
  ShowClassifiedForm(TForm2);
  //This will modal show an instance of a "TMyFormTwo" as a
  //sub class of TForm2, thus showing the Titel at first...
  ShowClassifiedForm(TMyFormTwo);
end;

end.




Best Regards
SwissDelphiCenter Team
www.swissdelphicenter.ch
[automatisch generierte EMail]


----------------------------------------------------
ENGLISH NEWSLETTER
----------------------------------------------------

Hi,

This new tip is new available on SwissDelphiCenter.ch:

Visit the programming tips at http://www.swissdelphicenter.ch/en/tipsindex.php


Rate this tip after skill, useful and overall at
http://www.swissdelphicenter.ch/en/showcode.php?id=1031



Author:
Loïs Bégué <delphi@xxxxxxxx>
www.begue.de


-------------
...create/manage any forms only through the Classname ?
-------------
Category: VCL



{
  Wenn Sie verschiedene Formular zentral initialisieren möchten
  und dies mittels der Klassennamen erledigen möchten, dann
  genügt folgendes...
  Dabei können Sie auch allgemeine Aufgaben dazu erledigen...
}

{
  If you'd like to zentralize Form creation only using class reference
  information, then use the following...
  You should do some generic jobs by the way, as shown in the example.
}

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

{ Main Form  declaration }
type
  TForm2 = class(TForm)
    btShowOneForm: TButton;
    procedure btShowOneFormClick(Sender: TObject);
  private
  public
  end;

var
  Form2: TForm2;


  { extra stuff for the example...}

{
  First, I declare some generic stuff to be available from all
  of my automatic created forms: interface is the best
}
type
  IMyGenericFormInterface = interface(IUnknown)
    ['{FDABF2E0-2279-11D6-9648-000374890932}']
    function GetTitel: string;
    property Titel: string read GetTitel;
  end;
{
  Then I can inflate the functionality of any of my declared
  Form classes, adding a ref to my generic interface.
  Notice: the function must be implemented in all the children
  classes using he interface, but the main program doesn't care
  what is there being done...just use it.
}

type
  TMyFormTwo = class(TForm2, IMyGenericFormInterface)
  private
    function GetTitel: string;
  end;
{
  You have to register your form class using RegisterClasses
  or to declare a variable of the given class so Delphi'll
  do it for you...
}
var
  MF2: TMyFormTwo;
  {...END of extra stuff }

implementation

{$R *.dfm}


function TMyFormTwo.GetTitel: string;
begin
  Result := 'Titel of 2. Form';
end;

{ !!! THIS is the "Global creation procedure" you should use  !!! }
procedure ShowClassifiedForm(TheFormClass: TFormClass);
var
  X: TForm;
begin
  X := TheFormClass.Create(Application);
  try
    // Extra stuff: Check to see if TheFormClass
    //              can manage with Title
    if Supports(X, IMyGenericFormInterface) then
      ShowMessage((X as IMyGenericFormInterface).Titel);

    X.ShowModal;
  finally
    X.Free;
  end;
end;

procedure TForm2.btShowOneFormClick(Sender: TObject);
begin
  //This will modal show a new copy a the Form2...
  ShowClassifiedForm(TForm2);
  //This will modal show an instance of a "TMyFormTwo" as a
  //sub class of TForm2, thus showing the Titel at first...
  ShowClassifiedForm(TMyFormTwo);
end;

end.



Best Regards
SwissDelphiCenter Team
www.swissdelphicenter.ch
[automatic generated EMail]

Other related posts:

  • » New Tip - ...create/manage any forms only through the Classname ?