Link to home
Start Free TrialLog in
Avatar of girona
girona

asked on

Calling and creating forms at run time

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.
Avatar of StevenB
StevenB

 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.

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
ASKER CERTIFIED SOLUTION
Avatar of d003303
d003303

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of girona

ASKER

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.
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;
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
Avatar of girona

ASKER

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.
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