Board index » delphi » Very weird problem with FindComponent.

Very weird problem with FindComponent.

Hi,

I want to use this line in a procedure:
Edit := TEdit(FindComponent('Edit' + IntToStr(i + 1)));
The Classes unit is mentioned by Uses. The line above works if I put
it in the TForm1.FormCreate procedure. The compiler gives no error.
But if I put it in the procedure were I need it(same unit), the
compiler gives 'Undeclared indentifier : FindComponent'. If I put the
mouse-cursor on the FindComponent, nothing happens --> normally you
see the unit FindComponent belongs to and the source-line.
I also use TMemoryStream in this same procedure and it uses also
Classes. But the compiler doesn't give an error with the MemoryStream.
How is this possible?
Many thanks for your help.

Regards,
Ad van de Laar

 

Re:Very weird problem with FindComponent.


Hi !

FindComponent is a method of TComponent, not a plain function. So, if you use it outside a TForm
method, you must specify on what component to invoke it:

e.g.:  SomeEdit:=TEdit(Form1.FindComponent('Edit1'));

--

Bjoerge

<<Ad van de Laar skrev i meldingen <37a59eba.2561...@news.xs4all.nl>...
Hi,

I want to use this line in a procedure:
Edit := TEdit(FindComponent('Edit' + IntToStr(i + 1)));
The Classes unit is mentioned by Uses. The line above works if I put
it in the TForm1.FormCreate procedure. The compiler gives no error.
But if I put it in the procedure were I need it(same unit), the
compiler gives 'Undeclared indentifier : FindComponent'. If I put the
mouse-cursor on the FindComponent, nothing happens --> normally you
see the unit FindComponent belongs to and the source-line.
I also use TMemoryStream in this same procedure and it uses also
Classes. But the compiler doesn't give an error with the MemoryStream.
How is this possible?
Many thanks for your help.

Regards,
Ad van de Laar>>

Re:Very weird problem with FindComponent.


It's actually quite simple.
FindComponent is a method of TComponent.
There you have to specify which component the FindComponent belongs to.
Inside a form event the compiler knows that FindComponent is a method of
TForm.
But outside a TComponent procedure the FindComponent method is not known.
So to make it works outside the Form class you should write something like
  Edit := TEdit(Form1.FindComponent('Edit' + IntToStr(i + 1)));
Note the use of Form.

Ad van de Laar <argo...@nospam.xs4all.nl> skrev i en
nyhedsmeddelelse:37a59eba.2561...@news.xs4all.nl...

Quote
> Hi,

> I want to use this line in a procedure:
> Edit := TEdit(FindComponent('Edit' + IntToStr(i + 1)));
> The Classes unit is mentioned by Uses. The line above works if I put
> it in the TForm1.FormCreate procedure. The compiler gives no error.
> But if I put it in the procedure were I need it(same unit), the
> compiler gives 'Undeclared indentifier : FindComponent'. If I put the
> mouse-cursor on the FindComponent, nothing happens --> normally you
> see the unit FindComponent belongs to and the source-line.

Other Threads