Solved

What causes "A component <> already exists" error?

Posted on 2003-11-11
6
231 Views
Last Modified: 2012-05-04
What causes "A component <> already exists" error?

MainFrm;

procedure TMainFrm.CallForm1Execute(Sender: TObject);
begin
  if not Assigned(Form1) then
     Form1:=TForm1.Create(application);
  Form1.Show;
end;

---------------------------------------------------------------------------------------

Form1;

procedure TForm1.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=cafree;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Form1:=nil;
end;

Usually this works but there are times that the error ("A component Form1 already exists" ) pops out ...


Thanks!
0
Comment
Question by:ttanyag
  • 2
6 Comments
 
LVL 17

Accepted Solution

by:
geobul earned 100 total points
Comment Utility
Hi,

I can't reproduce that error here. I have tried another way which worked well for me:

1.The calling part is the same (in your MainFrm).

2. In Form1

procedure TForm1.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  self.Release;
end;

and without Destroy event.

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
Comment Utility
Oops, copied it in a wrong way. Sorry. Should be:

procedure TForm1.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  self.Release; // or Form1.Release;
  FreeAndNil(Form1);
end;

Regards, Geo
0
 
LVL 12

Assisted Solution

by:andrewjb
andrewjb earned 100 total points
Comment Utility
I've had this when:

a) I'm inheriting one form from another
and
b) I've got two .dfm files called the same, and the compiler is getting confused and picking up the wrong one.

So, make sure for your form, that the xxx.dfm file only exists in one place, and that you haven't got two units with the same name somehow.


Andrew.
0
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 100 total points
Comment Utility
just to say,
that it is bad practice to use a reference-var
in its own methods

action := caFree; //is ok -> calls internal Release

>the error ("A component Form1 already exists" )
that you get this may caused by a too short create-call after the release,
because release sends a message to itself, and it could be,
that this message is not arrived, when you try to create the form new

i myself prefere to lookup the screen.forms-list for evaluating,
if a form is already there, rather than a test of nil to the reference-var

meikl ;-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sending Gmail through Delphi 3 69
Intraweb download file link ? 1 88
find a node in VST 2 46
Base1 Encode/Decode 3 0
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…
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…
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

9 Experts available now in Live!

Get 1:1 Help Now