Solved

Form creation procedure

Posted on 2002-07-28
15
177 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

895 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

15 Experts available now in Live!

Get 1:1 Help Now