Solved

Form creation procedure

Posted on 2002-07-28
15
175 Views
Last Modified: 2010-04-04

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
0
Comment
Question by:jbeh
  • 8
  • 4
  • 2
  • +1
15 Comments
 
LVL 1

Expert Comment

by:Dumani
ID: 7183178
procedure makeform(Form: Tfm_execs);
begin
  Form := Tfm_execs.Create(nil);
  Form.Show;
end;
0
 

Author Comment

by:jbeh
ID: 7183211
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7183402
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
 

Author Comment

by:jbeh
ID: 7183542
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
 

Author Comment

by:jbeh
ID: 7183685
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7183779
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
 

Author Comment

by:jbeh
ID: 7183819
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
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.

 

Author Comment

by:jbeh
ID: 7183824
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
 

Author Comment

by:jbeh
ID: 7183825
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 150 total points
ID: 7183909
:-) glad you like it
0
 

Author Comment

by:jbeh
ID: 7183933
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
 
LVL 1

Expert Comment

by:Dumani
ID: 7184467
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
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7185044
lovely :)
0
 

Author Comment

by:jbeh
ID: 7185574
Wonderful solution -

Thank you VERY much.

Points transferred as promised

John
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7185954
:-) was a pleasure for me

good luck again

meikl ;-)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

14 Experts available now in Live!

Get 1:1 Help Now