Solved

Calling and creating forms at run time

Posted on 1998-04-27
8
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

695 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