Solved

Calling and creating forms at run time

Posted on 1998-04-27
8
210 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi with SQL Natvie Client 15 86
Activex loadlibrary and show parented form issue 6 286
Best Firemonkey component pack 1 87
tidtcpserver connection lost handle 2 71
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now