Solved

Check if a Form is Open ?

Posted on 2001-07-02
19
482 Views
Last Modified: 2010-04-06
Hi,

hopefully this is an easy one.

How do I check if a Form is open within Delphi 5 ?

I have a main menu, which opens several forms, but if I click any menu option more than once, multiple instances of the form open. The app is NOT an MDI app.

I have tried searching Experts-Exchange, but am unable to view past the 1st page of results.


Thanks
pjelias
0
Comment
Question by:pjelias
[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
  • 5
  • 5
  • 3
  • +3
19 Comments
 
LVL 6

Expert Comment

by:edey
ID: 6246947
well, you could try looking for it with the findWindow api, though I would think the answer would be to try & never instantiate it more then once.  When show it you could try something like:

if form1 = nil then
 form1 := TForm1.create(some_owner);
form1.show;

GL
Mike
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6247177
This is how I do it and it basically is the same as edey's comment just different syntax

if not Assigined(form1) then
     form1 := TForm1.create(nil);

or

if not Assigined(form1) then
     form1 := TForm1.create(Owner_of_Form);
0
 

Author Comment

by:pjelias
ID: 6247217
Have tried both, neither works. Form will not open at all.
Do I have to initialise form1 first (vwForm in my case).

My code below.

var sysUser: String;
    vwForm: TForm;
    rwAccess : String;
begin
  sysUser:=LowerCase(SysInfo.CurrentUser);
  case menuBar.SelectedIndex of
  0:begin
     rwAccess:=AccessType(sysUser,'BUILDUP');

     if rwAccess='RW' then begin
        if not Assigned(vwForm) then begin
          vwForm:=TFormBuildUp.Create(Self,[arWrite]);
          vwForm.Show;
        end;
     end;
  ... other code below.

When I track the vwForm variable I get the following

vwForm: ([csInheritable], True, False, $5C7DA4, [fsCreatedMDIChild])

All Other code works until I add the code re: Checking if Form is opened.

Any ideas ?
0
Technology Partners: 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!

 
LVL 44

Accepted Solution

by:
CrazyOne earned 50 total points
ID: 6247239
Is the form auto created when the application fires up? Also take the Show part out side of the "if not Assigned(vwForm) then begin" because all you want this statement to do is to see if the form is Assigned or not and what ever the case is you still want it to show. As you have it now it will only show if the form is not Assigned.
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6247246
I got to hit the sack so I will check back on this thread after I get some sleep.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6247389
you could iterate through the mainforms-childforms,
and compare its classname like

function mainform.formexists : TForm;
begin
  result := nil;
  for i := 0 to mdichildcount - 1 do
    if mdichilds[i] is TFormBuildUp then
      result := forms[i];
end;

and you could it call like

aform := formexists;
if aform = nil then
  with TFormBuildUp.Create(self) do
     show
else
  aform.show;

(just from head, syntax errors possible)

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6247474
oh, no mdichilds,
then use
formcount instead of mdichildcount,
forms[] instead of mdichilds[]

meikl ;-)
0
 

Author Comment

by:pjelias
ID: 6247485
CrazyOne,

How Obvious ?

I need to get away from my pc every now and then, and relook at the code after a while.

The code I had worked, which is similar to Kretzschmar's code, but as I stated, it is not an MDI App.

The problem was that the Form was included in the Apps Autocreate method, which I moved, and now works OK, as CrazyOne suggested.


Thanks to all.
0
 
LVL 3

Expert Comment

by:Stefaan
ID: 6247518
Hi,

Well Meikl, will this also work with a non MDI application ?

Pjelias,

Are you using the sample form I posted in the other question ? If so, add the following line to the template form :

unit ARSample;

interface

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

type
  { The different types of Access Right }
  TAccessRight = ( arRead, arWrite, arAdd, arRemove );
  { Will be used to hold any combination of TAccessRight }
  TAccessRights = set of TAccessRight;

  TForm1 = class(TForm)
    btnEdit: TButton;
    btnPost: TButton;
    btnCancel: TButton;
    btnDelete: TButton;
    btnDuplicate: TButton;
    btnInsert: TButton;
    btnButton1: TButton;
    procedure btnButton1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    FAccessRights: TAccessRights;
    procedure SetAccessRights(const Value: TAccessRights);
    { Private declarations }
  protected
    procedure DisableButtons;
  public
    { Public declarations }
    Constructor Create( AOwner : TComponent; ARights : TAccessRights ); reintroduce; overload;
    property AccessRights : TAccessRights read FAccessRights write SetAccessRights;
  end;

<B>  TAccessRightsFormClass = Class of TForm1;</B>

Where you should replace TForm1 with the Class of the form you are using.  Then in the main form put the unit in the interface uses clause ( the topmost one ) and remove it from the implementation uses clause.

Next create a new method in your main form it should look like this :

unit MainForm;

interface

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

type
  TForm2 = class(TForm)
    btnButton1: TButton;
    procedure btnButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure ShowOrCreateForm( aFormClass : TAccessRightsFormClass; AccessRights : TAccessRights );
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

procedure TForm2.btnButton1Click(Sender: TObject);
begin
  ShowOrCreateForm( TForm1, [ arRead, arWrite ] );
end;

{*****************************************************************************
  Name           : TForm2.ShowOrCreateForm
  Author         : Lesage Stefaan
  Arguments      : aFormClass   - The class of the form you are looking for
                   AccessRights - The Access rights which should be used to
                                  create a new form if it isn't found.
  Return Values  : None
  Exceptions     : None
  Description    : This method will loop over all forms in the application to
                   find a form of the given class.  If such a form is found, it
                   will be shown.  If none was found, one will be created with
                   the given access rights and shown.
  History        :

  Date         By                   Description
  ----         --                   -----------
  03/07/2001   Lesage Stefaan       Initial creation of the Procedure.
 *****************************************************************************}

procedure TForm2.ShowOrCreateForm(aFormClass: TAccessRightsFormClass; AccessRights : TAccessRights);
var
  lcv   : Integer;
  Found : Boolean;
  aForm : TForm;
begin
  { Initialise the Variable }
  Found := False;
  aForm := Nil;

  { Loop over all forms of the application }
  for lcv := 0 to Pred( screen.FormCount ) do
  begin
    { Check if the form is of the given class }
    if Screen.Forms[ lcv ] is aFormClass then
    begin
      { If so Show it }
      aForm := Screen.Forms[ lcv ];
      Found := True;
    end;
  end;

  { If a form of our formclass was not found, create it }
  if ( Not Found ) then
  begin
    aForm := aFormClass.Create( Self, AccessRights );
  end;

  if ( Assigned ( aForm ) ) then
  begin
    aForm.Show;
  end;
end;

end.

As you see I added a button to my form which calls the method I created.  You should simply pass the class of the form you are looking for and the access rights which should be used to create a new form if none was found.

Best regards,


Stefaan
0
 
LVL 3

Expert Comment

by:Stefaan
ID: 6247521

Just a few seconds too late ;-)
0
 

Author Comment

by:pjelias
ID: 6247611
Stefaan,

just got a message re: the code you posted, took a look, it looks a bit cleaner than what I have done.

Question - if I was to open say 4-5 Forms at the same time (may be possible), using the above method, would it matter If the same Form variable was used, while Forms remained open - aForm.


pjelias
0
 
LVL 3

Expert Comment

by:Stefaan
ID: 6247715
Hi,

Well the method I have given doesn't use any form variable at all.  It will check if a given Form Class is found.  The only drawback is that you won't be able to have two instances of a TForm1, but I thought that was exactly what you wanted to have.

Best regards,


Stefaan
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6248132
.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6248134
.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6248171
It's really easy to steal questions  :o)

Sorry for the previous comments...
0
 
LVL 3

Expert Comment

by:Stefaan
ID: 6248226
Steal questions ? What do you mean by that ?

Best regards,


Stefaan
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6248283
Buying a question without paying. I'm not going to make a habit of it, don't worry   :o)
0
 
LVL 3

Expert Comment

by:Stefaan
ID: 6248326
And, did you find a good answer ?
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6248358
On the question "Can I steal a question?": Yes!
On the question "Check if a Form is Open?": I didn't look  :o)
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

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…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
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…

728 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