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
Solved

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

Posted on 2003-11-11
6
234 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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 prevent click fast 2 238
Communication Between RC4 Delphi <-> PHP 3 123
How to define IfThen functions in one common unit? 4 60
Find and Replace Stream with 0s 8 69
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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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