Solved

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

Posted on 2003-11-03
14
448 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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