Solved

Form creation procedure

Posted on 2002-07-28
15
183 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
[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
  • 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
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: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
 

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

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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