Solved

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

Posted on 2003-11-03
14
445 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
  • 3
  • 2
  • 2
  • +4
14 Comments
 
LVL 5

Accepted Solution

by:
arjanh earned 50 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 50 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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 50 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 50 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 50 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

806 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