Form creation procedure


I have a lot of buttonclick events which create subsidiary forms of the following kind.

procedure TfmMain.Button1Click(Sender: TObject);
begin
  inherited;
  if fm_execs = nil then begin
    fm_execs := Tfm_execs.create(Application.MainForm);
    fm_execs.Show;
  end else begin
    fm_execs.BringToFront
  end;
end;


What I want to do is replace the button1click details with something like


procedure TfmMain.Button1Click(Sender: TObject);
begin
  makeform(fm_execs);
end;

My problem is - How do I write the makeform procedure ?

Please note the "Inherited" refers to visual form inheritance and I need this because it does a lot of ground work in sizing placement fonts and various other bits and pieces which I want to retain.  

I have struggled with this without success on and off for some time now and the time has come when I have to accept defeat and ask for help. :-)).

Thank you - John
jbehAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
kretzschmarConnect With a Mentor Commented:
:-) glad you like it
0
 
DumaniCommented:
procedure makeform(Form: Tfm_execs);
begin
  Form := Tfm_execs.Create(nil);
  Form.Show;
end;
0
 
jbehAuthor Commented:
Ah yes - Fine - I agree BUT

Perhaps I didn't ask my question correctly - Sorry

If I do what you suggest I am going to need a different procedure for every form.  

What I am looking for is a single procedure that I can call dozens of times - Rather than dozens of procedures that I call once each with different forms being created.

I.e.

Makeform (form01);
Makeform (form02);
Makeform (form03);
Makeform (form04);


Does that make sense ?.



Thanks John
 
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
kretzschmarCommented:
maybe this helps a bit

unit unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    Procedure MakeForm(FormClassName : String);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses unit2;  //add the Units, where the Other FormClasses are defined

{$R *.DFM}

Procedure TForm1.MakeForm(FormClassName : String);
begin
  Try
    with TFormClass(GetClass(FormClassName)).Create(self) do
    begin
      //maybe more settings
      show;
    end;
  except
    raise Exception.Create('Error Creating FormClass '+FormClassName);
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  makeform('TForm2');  //Make a form
end;

initialization
  RegisterClass(TForm2);
  //register other classes
end.

just as kickoff

meikl ;-)
0
 
jbehAuthor Commented:
Meikl

Hi -

Thank you for that - That seems to work fine.

What I now need to deal with is
a) The inheritance.
b) The single instance.

I'll play about a bit and see if I can solve this but the basic works fine -

John
0
 
jbehAuthor Commented:
OK

The inheritance problem is solved
or more accurately -
I don't have an inheritance problem.

The creation works absolutely fine.

Now all I need is to solve the single instance issue.

John
0
 
kretzschmarCommented:
maybe something like this

Procedure TForm1.MakeForm(FormClassName : String);
var
  i : Integer;
  found : Boolean;
begin
  //search if there is a form of same Class
  found := false;
  i := 0;
  while (I < screen.FormCount) and (not found) do
    If screen.forms[i].ClassNameIs(FormClassName) then
      found := true
    else
      inc(i);
  //if there is one of same Class then show it
  if found then
    screen.forms[i].Show
  else
  //otherwise create it
  Try
    with TFormClass(GetClass(FormClassName)).Create(self) do
    begin
      show;
      //maybe more
    end;
  except
    raise Exception.Create('Error Creating FormClass '+FormClassName);
  end;
end;

meikl ;-)
0
 
jbehAuthor Commented:
meikl


Thanks for that -

I have been working on the same problem and I am SURE yours is better than mine. However for what it is worth I came up with

procedure TfmMDIParent.MakeForm(FormClassName: string);
var
  x                           : integer;
  exists                      : boolean;
begin
  exists := false;
  for x := 0 to componentcount - 1 do begin
    if Components[x].ClassNameIs(FormClassName) then begin
      exists := True;
      break;
    end;                                          
  end;                                            
  if not exists then begin
    with TFormClass(GetClass(FormClassName)).Create(Application.MainForm) do begin
      show;
    end;
  end else begin
    //      TFormClass(GetClass   **************(FormClassName)).BringToFront;
  end;
end;


**********   I'm struggling to get "BringtoFront to work at this point"

I've cut out the try / except for brevity - I'll keep it in for "real" use.


I am now VERY close to getting precisely what I am looking for


Thank you for your continued assistance.

John



0
 
jbehAuthor Commented:
Hi again

I've now read through what you suggest and yours seems intuitively much better than mine. :-( !!

Do you know if

   screen.forms[i].Show has the effect of "bring to front"

Actually it wouldn't surprise me if

screen.forms[i].Bringtofront works.


Back to my experimentation !!

John


0
 
jbehAuthor Commented:
Hi

screen.forms[i].Show  --  "bring to front"

Yes it does appear to have this effect.

I think your code is better than mine for at least 2 reasons

1) Bringtofront works.
2) Iterating through screen objects is likely to be a lot more efficient than iterating through components.


John
0
 
jbehAuthor Commented:
Hi meikl

Thanks a lot -

I've been looking at this problem on and off for ages

I'm really pleased to have got a reasonable solution.

I'll leave the question open for a short time (just in case) but my guess is that this is done.

All other things being equal I'll send the points within the next 24 hours.

Once again thanks -

John
0
 
DumaniCommented:
ibleh,

  if you are really happy and code works then why don't you give points to someone who solved your problem? Just click Accept button near the answer.
0
 
Lee_NoverCommented:
lovely :)
0
 
jbehAuthor Commented:
Wonderful solution -

Thank you VERY much.

Points transferred as promised

John
0
 
kretzschmarCommented:
:-) was a pleasure for me

good luck again

meikl ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.