[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2003-11-11
6
Medium Priority
?
244 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
4 Comments
 
LVL 17

Accepted Solution

by:
geobul earned 400 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 400 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 400 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

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.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month20 days, left to enroll

873 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