Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Only one form of each type

Posted on 2002-03-16
6
Medium Priority
?
134 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
[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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

610 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