Solved

Only one form of each type

Posted on 2002-03-16
6
127 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now