Solved

Only one form of each type

Posted on 2002-03-16
6
129 Views
Last Modified: 2010-04-05
How can I find out of a form already exists?  I have a form that is created at runtime (click on a button => 'TfrmExample.create(Self)').  When the button is clicked, but the form already exists, there may not be created a second one.  Only one form of each type is allowed?  How can I control this.  It may not be done with MDIforms and MDIchild forms.  Al the forms are 'fsNormal'.

Dirk.

PS sorry for my poor English.
0
Comment
Question by:ka1a
  • 3
  • 3
6 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 6871911
hi,
check if the form variable you used to create form is assigned
ie:

var
someform : tform;

---------- later on ---

if assigned(someform) then
//form exists already dont create
else
//form dont exist yet so can be created


(note:
remember to free the form when finished before using it again otherwise someform will still be assigned)
0
 

Author Comment

by:ka1a
ID: 6872024
hi inthe,

I tried with assigned.  It seems that it don't work.  I get always the message the form already exists.  The code folows below:

procedure TfrmMain.SpeedButton1Click(Sender: TObject);
var LlnFiche:TfrmLlnFiche;
begin
  if assigned(LlnFiche) then
    caption:='Already exists' else
  begin
    LlnFiche:=TfrmLlnFiche.Create(Self);
    LlnFiche.Create(Self);
  end;
end;

What am I doing wrong?

Thanks in advance.

Dirk.

PS sorry for my poor English.
0
 
LVL 17

Expert Comment

by:inthe
ID: 6872194
hi,
variable must be global ,here an example:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, 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;
  form2 : tform;

  implementation

{$R *.DFM}

uses unit2;

procedure TForm1.Button1Click(Sender: TObject);
begin
if assigned(form2) then
form1.caption := 'form2 already exists'
else begin
form2 := tform2.Create(self);
form2.show;
end;
end;

end.









unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

uses Unit1;

{$R *.DFM}

procedure TForm2.Button1Click(Sender: TObject);
begin
close;
end;

end.
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:ka1a
ID: 6872351
Last question:

It seems to work.  But when I close form 2 and then try to create it back by clicking the button on form 1, I get the message that form 2 already exists.  I think that I need to free the variable form2 when form2 is closed.  How can I do that and where to I put the code.  I tried to do 'form1.form2.free' in the onclose event of form2 but then I get an error message.

Thanks again for al your help.

Dirk.
0
 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
ID: 6872559
hi,
this is better,doesnt require the vars ,it will look for form2 and if it finds it it will set it to top (form2.show) and if it doesnt find it it will create it:

uses Unit2;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  f2,i:Integer;
begin
  f2 := 0;
  for i := 0 to Screen.FormCount -1 do
    if Screen.Forms[i] is TForm2 then
      f2 := i;
  if f2 >= 0 then
    Screen.Forms[f2].Show
  else begin
    Form2 := TForm2.Create(Self);
    Form2.Show;
  end;
end;
0
 

Author Comment

by:ka1a
ID: 6872747
Thanks Inthe,

I learned a lot.  It works great.  You where a great help.

Thanks again.

Dirk.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

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…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

778 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