Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: TFormClass - how to check if a form exists ?



Permlink Replies: 2 - Last Post: Feb 26, 2018 2:41 AM Last Post By: Jose Silva
Jose Silva

Posts: 20
Registered: 11/1/11
TFormClass - how to check if a form exists ?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 25, 2018 11:06 AM
Dear all

I've been trying to implement a method to create a form if it doesn't exist or to show it . This method will be called in the AtcionList actions linked to ToolButtons of a TToolbar.

The critical part is how to check if a given parameter TFormClass passed in the method call has any Form instance of it already created or not.

Below is the code I've implement to perform the what I wanted, focus on TFormHome.PR_CreateOpenForm(PClassform : TFormClass) :

unit UnitFormhome;
 
interface
 
Uses 
    .....
 
 
type
  TFormHome = class(TForm)
    ActionList1: TActionList;
    Action1: TAction;
    Action2: TAction;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    PanelHome: TPanel;
    Action3: TAction;
    ToolButton3: TToolButton;
    procedure Action1Execute(Sender: TObject);
    procedure Action2Execute(Sender: TObject);
    procedure PR_CreateOpenForm(PClassform : TFormClass);
    procedure Action3Execute(Sender: TObject);
 
  private
    { Private declarations }
 
  public
    { Public declarations }
  end;
 
var
  FormHome: TFormHome;
 
implementation
 
uses
    UnitFormA,
    unitFormB,
    UnitFormC;
{$R *.dfm}
 
 
 
procedure TFormHome.PR_CreateOpenForm(PClassform : TFormClass);
// This routine itends to create a new form instance if it does not exist OR 
// open (show) a form if it already exist 
//  
var
    vform : TForm;
 
begin
      if Pclassform is nil then   // check if there is an instance of PClassForm of a given TFormClass   - HOW TO DO THAT ?
     begin
           vform              := Pclassform.Create(Application);
     end;
     vform.Parent           := PanelHome;
     vform.Align              := alclient;
     vform.BorderIcons  := [biSystemMenu];
     vform.BorderStyle   := bsNone;
     vform.Show;
     vform.SetFocus; 
end;
 
 
procedure TFormHome.Action3Execute(Sender: TObject);
// Action3 called from ToolButton3
// It is passed the parameter TFormC class 
begin
      PR_CreateOpenForm(TFormC);
end;
 


I appreciate your support and directions on that !

Edited by: Jose Silva on Feb 25, 2018 11:06 AM
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: TFormClass - how to check if a form exists ? [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 25, 2018 11:17 PM   in response to: Jose Silva in response to: Jose Silva
Jose Silva wrote:

I've been trying to implement a method to create a form if it
doesn't exist or to show it . This method will be called in the
AtcionList actions linked to ToolButtons of a TToolbar.

The critical part is how to check if a given parameter TFormClass
passed in the method call has any Form instance of it already created
or not.

The global Screen object has a list of the currently existing forms;
there is code in the VCL TForm class that registers a form with the
Screen object when it is created and unregisters it when it is
destroyed.

Function FindOrCreateFormByClass( aFormClass: TFormClass ): TForm;
Var
i: Integer;
Begin
For i:= 0 To Screen.formCount - 1 Do Begin
Result := Screen.Forms[i];
If Result.ClassType = aFormClass Then
Exit;
End;
Result := aFormClass.Create( Application );
End;

--
Peter Below
TeamB
Jose Silva

Posts: 20
Registered: 11/1/11
Re: TFormClass - how to check if a form exists ?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 25, 2018 11:57 PM   in response to: Jose Silva in response to: Jose Silva
Jose Silva wrote:
Dear Peter

I've followed your sugestion , it worked fine !

Thanks a lot for the quick help !

Edited by: Jose Silva on Feb 26, 2018 2:41 AM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02