Board index » delphi » Multiple instances of forms

Multiple instances of forms

This is an elegance question for an existing application.

CHALLENGE:  We want to be able to open multiple instances of forms
to show multiple data items concurrently.  In order to do this without
creating a new TForm descendant, we have chosen to have our security
module keep up with the multiple instances, as it already manages
form creation.

PROBLEM:  Some popup forms, most of standard TForm class, need to
reference datasets on the parent form.

Is the best solution to:

Parent Module:
        var fmParent: TfmParent;
        ...
        fmChild.Create (Self);

Child Module:
        var Parent: TfmParent;
        ...
        OnCreate (Sender: TObject);
        begin
                Parent := TfmParent(Sender);
                Parent.taTable.Open; {e.g}
        end;

On a deeper level, when a form is created by the IDE, a variable
of type TForm is always created in the class (as opposed to the
implementation area) which is usually passed to the Application.Create
method.  Is this a class or instance variable?  (I know, RTFM).

In the sample code cited above, would the following work for
multiple instances?

Parent:
        var
                fmParent: TfmParent;
        ...
        fmParent := Self;
        fmChild.Create (fmParent);

Child Module:
        uses ParentUnit;
        ...
        OnCreate (Sender: TObject);
        begin
                fmParent.taTable.Open;
        end;

Remember, ther will be multiple instances of this form.  Which is the
safest, most reliable method.

TIA,
John Martin

 

Re:Multiple instances of forms


Quote
On Tue, 18 Mar 1997 15:10:51 -0500, jcm_arc...@minspring.com wrote:
>This is an elegance question for an existing application.

>CHALLENGE:  We want to be able to open multiple instances of forms
>to show multiple data items concurrently.  In order to do this without
>creating a new TForm descendant, we have chosen to have our security
>module keep up with the multiple instances, as it already manages
>form creation.

>PROBLEM:  Some popup forms, most of standard TForm class, need to
>reference datasets on the parent form.

>Is the best solution to:

>Parent Module:
>    var fmParent: TfmParent;
>    ...
>    fmChild.Create (Self);

>Child Module:
>    var Parent: TfmParent;
>    ...
>    OnCreate (Sender: TObject);
>    begin
>            Parent := TfmParent(Sender);
>            Parent.taTable.Open; {e.g}
>    end;

>On a deeper level, when a form is created by the IDE, a variable
>of type TForm is always created in the class (as opposed to the
>implementation area) which is usually passed to the Application.Create
>method.  Is this a class or instance variable?  (I know, RTFM).

It is actually a global pointer to the instance of the form created within the
Application.CreateForm(...) call.

- Show quoted text -

Quote
>In the sample code cited above, would the following work for
>multiple instances?

>Parent:
>    var
>            fmParent: TfmParent;
>    ...
>    fmParent := Self;
>    fmChild.Create (fmParent);

>Child Module:
>    uses ParentUnit;
>    ...
>    OnCreate (Sender: TObject);
>    begin
>            fmParent.taTable.Open;
>    end;

>Remember, ther will be multiple instances of this form.  Which is the
>safest, most reliable method.

>TIA,
>John Martin

One of the things I don't really like about Delphi (and this list is very small)
is that it thinks I always want to create every form within the application at
application startup.  Here is a tip; don't do this when your app has over 100
forms.  This is also true for most modal forms.  It seems to be a waste of
resources and memory when you create a modal form at app startup when it will
probably only be used once during the life of the application.  If this is the
case for several of your 'child' forms, I would suggest you do the following:

1) create a nice helper function to create and show your
   modal form from other forms
2) get rid of the automatically created variable for the
   modal forms (within the interface section)

Ex:

unit Splat;

interface

uses .....

// a nice handy-dandy helper func
function ShowMyCoolSplatDlg(SomeDataSet: TDataSet): Boolean;

type
  TCoolSplatDlg = class(TForm)
    ... delphi stuff here ...
  private
    FDataSetToUse: TDataSet;
    ...
  end;

// KILL ME !!
var
  CoolSplatDlg: TCoolSplatDlg;         // <<<==== KILL ME!!!!

implementation

function ShowMyCoolSplatDlg(SomeDataSet: TDataSet): Boolean;
begin
  with TCoolSplatDlg.Create(Application) do
  try
    // set up your forms hooks to the 'parents' dataset here
    FDataSetToUse := SomeDataSet;
    Result := ShowModal = mrOK;
  finally
    Release;
  end;
end;

Other Threads