Solved

Only one form of each type

Posted on 2002-03-16
6
131 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
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!

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Spellcheck in Webbrowser 1 51
DBCtrlGrid, Delphi, Scroll 8 60
Delphi, TImage, Png 2 31
Can Live bindings change TGrid Cell Colour ? 1 34
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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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