Solved

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

Posted on 2003-11-03
14
441 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Expert Comment

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

Assisted Solution

by:kretzschmar
kretzschmar earned 50 total points
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 6

Assisted Solution

by:swift99
swift99 earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Back up a half a step ... why do you need to know "if" it has been created?
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

18 Experts available now in Live!

Get 1:1 Help Now