Solved

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

Posted on 2003-11-03
14
446 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

791 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