Solved

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

Posted on 2003-11-11
6
237 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
[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
  • 2
6 Comments
 
LVL 17

Accepted Solution

by:
geobul earned 100 total points
ID: 9729801
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
ID: 9729852
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
ID: 9730401
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
ID: 9732858
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 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