keeping track of a form

hi all

in Form 1, i have got 1 button:

Button1.OnClick:
Form2 := TForm2.Create(Self);
Form2.Show;

Form2.OnClose:
Action := caFree;

this is the problem. the user clicks on the button once, leaves Form2 open, and clicks the button again. this will create 2 form2 forms. thus, i want to be able to detect if form2 has been closed or not. this method that i have tried will not work:

if Form2.visible then
  form2.setfocus
else
begin
  form2 := Tform2.Create;
  form2.show;
end

form2.visible will generate an exception if form2 already closed. the only way i got it going was:

try
  form2.setfocus;
except
  form2 := Tform2.Create;
  form2.show;
end;

however, using an exception to detect the presence of a form is not particularly nice. any other ideas?

PS: the reason for freeing the form is to save memory.

Keith
LVL 1
keithcslAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

keithcslAuthor Commented:
Edited text of question.
0
intheCommented:
Hello
you can do this:

Button1.OnClick:
button1.enabled:=false; <-- insert this
Form2 := TForm2.Create(Self);
Form2.Show;



on form2.close
 do button1.enabled:=true;<-- and this


Regards Barry
0
keithcslAuthor Commented:
inthe

nope. i'd like to keep the button free so that if the form is in the background, the user can click on the button to show it again.

Keith
0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

intheCommented:
ok i have an idea but im off to work so i post later if no one else has give an annswer by when i finish
later
0
CalvinDayCommented:
Use this code. Create two other forms. Call one TFirst and TSecond. When the user presses the first button, the CreateForm function check all forms to see if TFirst exist. If it does, the function merely Shows the form, otherwise it creates the form and it get shown.

unit umain;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}
uses
  uFirst,uSecond;

function CreateForm(fc:TFormClass; Owner:TComponent):TForm;
var
  i:integer;
begin
  for i:=0 to Screen.FormCount-1 do
    if Screen.Forms[i] is fc then
      begin
      Screen.Forms[i].Show;
      exit;
      end;

  Result:=TForm(fc.NewInstance);
  Result.Create(Owner);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  CreateForm(TFirst,Self);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  CreateForm(TSecond,Self);
end;
0
edeyCommented:
Why don't you just change:

if Form2.visible then
  form2.setfocus
else
begin
  form2 := Tform2.Create;
  form2.show;
end

to:

if Form2 <> nil then
  form2.setfocus
else
begin
  form2 := Tform2.Create;
  form2.show;
end


GL
Mike
0
intheCommented:
there goes my new suggestions  :-))

please reject my original answer
0
keithcslAuthor Commented:
edey

that will not work because the form is not set to nil after closing and freeing it.

CalvinDay

could u post ur comment as an answer? i have not tested it yet. will do so in a short while...

Keith
0
edeyCommented:
Actually I'm pretty sure that form2 will equal nil after calling form2.free, 'cause I just tried it.

GL
Mike
0
keithcslAuthor Commented:
edey

where do u call form2.free?

i am assigning Action := caFree in the OnClose event of form2, as stated in the question.

Keith
0
edeyCommented:
I assumed that you were as (from the original question) you said:

"PS: the reason for freeing the form is to save memory. "


if this is the case then it only makes sense to ensure that the form's reasources, have indeed, been freed

GL
Mike
0
keithcslAuthor Commented:
edey

if i am not mistaken, setting Action := caFree frees the form.

can u post ur code so that i can see how it works? this will save us all the confusion.

Keith
0
CalvinDayCommented:
caFree calls Release:
Release Posts a message CM_Release:
CMRelease calls Free:
Nowhere is the Form pointer set to nil. And you can't set the form to nil yourself because the pointer is needed during the free process.

Thanks, Use this function:

function CreateForm(fc:TFormClass; Owner:TComponent):TForm;
var
  i:integer;
begin
  for i:=0 to Screen.FormCount-1 do
    if Screen.Forms[i] is fc then
      begin
      Screen.Forms[i].Show;
      exit;
      end;

  Result:=TForm(fc.NewInstance);
  Result.Create(Owner);
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
keithcslAuthor Commented:
thanks for clarifying that Calvin.

and for the solution :)

Keith
0
CalvinDayCommented:
And thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.