?
Solved

How do I tell if a form has already been created?

Posted on 2003-11-03
14
Medium Priority
?
450 Views
Last Modified: 2010-08-05
I am creating a form and freeing it when I no longer need it. My problem is I am not sure how to tell if my form has been created already. It's a simple list box really, and if the user clicks the right button I end up with more than one instance of the form. Help?
0
Comment
Question by:Smokulas
[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
  • 3
  • 2
  • 2
  • +4
14 Comments
 
LVL 5

Accepted Solution

by:
arjanh earned 200 total points
ID: 9674130
You could set the form pointer to nil after freeing it. Later you can then check for the form variable being nil or not...
0
 
LVL 5

Expert Comment

by:arjanh
ID: 9674170
Alternatively, you could show the popup form modally (do a subform.ShowModal instead of subform.Show). Then the user must close the listbox subform before he can press any key on the main form. That wouls also solve your problem.
0
 
LVL 5

Assisted Solution

by:Hypoviax
Hypoviax earned 200 total points
ID: 9675599
I guess this may be abstract but it will work if you know the window caption of your form:

if findwindow(nil,pchar('FORM CAPTION HERE")=1 then//(or true i cant remember)
 showmessage('Form Loaded') //You could also set a variable to say yes open or no closed
else
 showmessage('Form not loaded')

The beauty about this is that it is simple. I hope it satisfys your problem,

Hypoviax
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Expert Comment

by:IamNewbie
ID: 9676929
May be try something like this
if Form1=nil then ....
0
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 200 total points
ID: 9677059
a simple sample

unit CheckFormExistence_main_u;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses CheckFormExistence_child_u;  //TForm2 is defined there

function isFormCreated(AClassName : String) : TForm;
var i : integer;
begin
  i := 0;
  result := nil;
  while (i < screen.FormCount) and
        (screen.Forms[i].ClassName <> AClassName) do
    inc(i);
  if i < screen.FormCount then
    result := screen.Forms[i];
end;



procedure TForm1.Button1Click(Sender: TObject);
var f : TForm;
begin
  f := isFormCreated('TForm2');
  if assigned(f) then
  begin
    f.Show;
    f.BringToFront;
    f.SetFocus;
  end
  else
  begin
    f := TForm2.Create(Self);
    f.Show;
  end;
end;

end.

meikl ;-)
0
 
LVL 2

Expert Comment

by:xxflip
ID: 9677850
if you're creating the form as follows:

Application.CreateForm(TForm, Form);
or
Form:=TForm.Create(Self);


then when you free it you would do:

Form.Free;
Form:=nil;

so to check if the form is created or not you would do:

if not Assigned(Form) then ...


It's a good idea to fo the creation as folows:

if not Assigned(Form) then Application.CreateForm(TForm, Form);

and then free it when you don't need it anymore, this way you will never have an error or a second instance of the same form.
0
 
LVL 6

Assisted Solution

by:swift99
swift99 earned 200 total points
ID: 9683280
I tend to use a singleton pattern for this sort of problem

I hide the constructor (make it private), make sure the destructor sets the instance variable to NIL, and expose a class level getter method for the instance variable.

TMyForm = class (TForm)
public
  class function getMyForm: TMyForm;
end;

implementation
var
    MyFormInstance: TMyForm = NIL;

function TMyForm.getMyForm: TMyForm;
begin
    if not Assigned (MyFormInstance) then
    begin
        MyFormInstance := TMyForm.Create (Application);
    end;
    result := MyFormInstance;
end;
0
 
LVL 2

Assisted Solution

by:Ray_Adams
Ray_Adams earned 200 total points
ID: 9684922
I use this procedure to run form

    procedure RunForm(Form:TFormClass;AllowMulti:boolean=false;frmCaption:string='');
Form - class of the form
AllowMulti - Allowa nulti instance (in this case i check Caption to find is there are any form already with this Caption, for example if i open some report "Monthly Report - January", i don't need it twice)
frmCaption - Caption of the form
----
If you just want to prevent new form to be created twicem just use RunForm(TfrmMyForm)
-----------
procedure TfrmMain.RunForm(Form: TFormClass; AllowMulti: boolean;
  frmCaption: string);
var i:longint;
    bFound:boolean;
    f:TForm;
begin

if not AllowMulti then
begin
    bFound:=false;
    f:=Self;
     for i:=1 to MDIChildCount do
         begin
         if MDIChildren[i-1].ClassName=Form.ClassName then
         begin
              bFound:=true;
              f:=MDIChildren[i-1];
              break;
         end;
         end;
 if not bFound
   then
   begin

        With
           Form.Create(Self) do
           begin
            FormStyle:=fsMDIChild;
             if frmcaption <> '' then
              caption := frmcaption;
           end;  
        //else Form.Create(Self);

   end
   else f.BringToFront;
end //Multi instance checking Caption only
else
begin
    bFound:=false;
    f:=Self;
     for i:=1 to MDIChildCount do
         begin
         if MDIChildren[i-1].Caption=frmCaption then
            begin
            bFound:=true;
            f:=MDIChildren[i-1];
            break;
            end;
         end;
 if not bFound
   then
   begin

        With
           Form.Create(Self) do
           begin
            FormStyle:=fsMDIChild
           end;
        //else Form.Create(Self);

   end
   else f.BringToFront;
end;
and in code i use
RunForm(TfrmMyForm,false);
may be it will help you.
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 9692319
Trust me . You don't need all this lengthy code. Use something simple such as my suggestion.

Regards,

Hypoviax
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9692351
Hypoviax,

your suggestion has three fallbacks
- more than one form with same caption in same application instance
- more application instances
- another application may have the same caption

just for clarify

meikl ;-)

0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 9697329
Very true krezschmar i did not think of this, thank you for your comment.

Smokulas do not accept my answer if the conditions expressed by krezschmar are true.

Regards,

Hypoviax
0
 
LVL 6

Expert Comment

by:swift99
ID: 9701466
Back up a half a step ... why do you need to know "if" it has been created?
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

777 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