Solved

Calling and creating forms at run time

Posted on 1998-04-27
8
211 Views
Last Modified: 2010-04-06
I have made a form which contains a button and when I click the button I want to open one form or another deppending on a string that contains the form name. The new form is a form that contain several components not a new empty form.
0
Comment
Question by:girona
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 4

Expert Comment

by:StevenB
ID: 1337150
 If your forms are auto created by the application then you need only use the Show method to display a form. If the forms are not auto created, then they can be simply created before the call to Show.
  To select the correct form to display I suggest that you add all the possible forms to a TList at the creation of your application main form. You can then select the correct form by looping through this list and matching against the form name.
  I've offered this as a comment which I'll be happy to expand upon if you need more advice, however I'm afraid I'll be unable to access a computer until Wednesday afternoon. Bear with me, or allow someone else to answer if you're in a hurry.

  Steven.

0
 

Expert Comment

by:BigMadDrongo
ID: 1337151
StevenB is correct.  After you have created the forms, use the show method, or ShowModal if you want the forms to be modal.  What I do is to put a function into the form class definition called ShowForm to allow results from this form to be passed back (you can access the components in the form as well).  The code for this function is along the lines of:

function TMyForm.ShowForm: boolean;
begin
  try
    ShowModal;
  finally
    Free;
    result := true;
  end;
end;

ShowModal returns results depending on what button is clicked to close it.  You can set the modal result of buttons to these values (e.g for a cancel button the modal result is set to mrCancel).  This is very useful if creating dialog boxes.

If you need any more help, just leave a coment

BigMadDrongo
0
 
LVL 4

Accepted Solution

by:
d003303 earned 100 total points
ID: 1337152
Yo,
say that you included several units with several form classes and they are not auto-created at start time. Only your main form is. Create a new application, drop a TButton and a TEdit on the empty form, paste this code and apply the OnClick event of the button.

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var AClass : TComponentClass;
    AComponent : TComponent;
begin
  if Edit1.Text = ''
   then Exit;
  AClass := TComponentClass(GetClass(Edit1.Text));
  if AClass = nil
   then Exit;
  AComponent := AClass.Create(Self);
  if (AComponent is TCustomForm)
   then (AComponent as TCustomForm).Show
   else if (AComponent is TWinControl)
         then (AComponent as TWinControl).Parent := Self;
end;

initialization
  RegisterClasses([TForm1, TButton, TEdit, TPanel]);

end.

You can enter one of the classes contained in the RegisterClasses function call. A class instance will be created on the fly.
Just put the RegisterClasses function in each unit that contains a form you want to generate in your app.

Slash/d003303
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:girona
ID: 1337153
I tried your unit and it works fine, it was very similar to another that I tried before but I need to call a procedure called Action instead of Show or ShowModal but when I try to call it appears an error. All forms have the same procedure name but different instructions.

I also need to know how can I notice if a form is already created or not.
0
 
LVL 4

Expert Comment

by:StevenB
ID: 1337154
Girona,

"I also need to know how can I notice if a form is already created or not."

Test to see if the form equals nil:

If Form1 = Nil then Form1.Create;

The important thing to remember is that when you free a component the resources it uses are disposed of, but its reference will not automatically be set back to nil. This must be done explicitly if you will be required to perform the above check subsequent to freeing the form:

Form1.Free;
Form1 := Nil;
0
 
LVL 4

Expert Comment

by:d003303
ID: 1337155
Yo,
if you have a method called Action in each form class, and you introduce this method in every class, Delphi will not find the right method entry. You will have to create a subclass first.

Start with an empty form. Name it, say, ActionForm. Add a method to its declaration like

type
  TActionForm = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
    procedure Action; virtual; abstract;
  end;

Now, save this unit in your gallery directory. Right-click on the form an say Add to repository and go through the dialog.
Now, go to File->New, select your new-defined form and click on inherit. Now change the declaration of your new form to

type
  TActionForm1 = class(TActionForm)
  private
    { Private declarations }
  public
    { Public declarations }
    procedure Action; override;
  end;

and use the Action method like before. You could also go to any form source and replace, say
  TMyForm = class(TForm)
with
  TMyForm = class(TActionForm)
and add the override directive to the declaration.
Now, modify my answer's code like

  if (AComponent is TActionForm)
   then (AComponent as TActionForm).Action
   else if (AComponent is TWinControl)
         then (AComponent as TWinControl).Parent := Self;

Now it should work.

To check if an instance is already created or not you could use

function Valid(Instance : TObject): Boolean;
var CheckObj : IUnknown;
begin
  Result := true;
  try
    Instance.GetInterface(IUnknown, CheckObj);
  except
    Result := false;
  end;
end;

and check with

if Valid(ThisForm)
 then ThisForm.Action;

Slash/d003303
0
 

Author Comment

by:girona
ID: 1337156
I've tried the valid function but I don't know how to pass the object, because when I pass the object is the AComponent that you said me in your answer. What I have to pass is the name not the object. Other solution could be to know how many instances of an object are created.
0
 
LVL 4

Expert Comment

by:d003303
ID: 1337157
Yo,
seems that you want to keep track of all created instances. So you can use the ComponentCount and Components property. Each component keeps a list of owned components. The owner of each component is specified in the Create method, i.e. Create(Self). So Self is the reference for all forms created at runtime. To check by name, walk through the component list and check its name by its ClassName property (which is a string property).

Slash/d003303
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question