Solved

Calling and creating forms at run time

Posted on 1998-04-27
8
209 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

11 Experts available now in Live!

Get 1:1 Help Now