Board index » delphi » Help: FindComponent can't find form components?

Help: FindComponent can't find form components?

Hi all,
  When using the following code TheComponent is always nil.  Is their
another way to do this?  

procedure TForm1.ButtonClick(Sender: TObject);
var
  TheComponent: TComponent;
begin
  TheComponent := FindComponent('Form1');
  if TheComponent <> nil then
    Button2.Caption := TheComponent.Name;
end;

 

Re:Help: FindComponent can't find form components?


On 16 Sep 1995 20:56:20 GMT, Bill Smith <smi...@baymont.com> wrote:

Quote
>Hi all,
>  When using the following code TheComponent is always nil.  Is their
>another way to do this?  

>procedure TForm1.ButtonClick(Sender: TObject);
>var
>  TheComponent: TComponent;
>begin
>  TheComponent := FindComponent('Form1');
>  if TheComponent <> nil then
>    Button2.Caption := TheComponent.Name;
>end;

FindComponent finds a named component that is owned by the sender.
Usually, the form is the owner, so FindComponent does not work.

Why not just refer to Form1 directly?  In the example, Form1is Self,
so there is no need to call FindComponent at all.  Also, in the
ButtonClick handler, Self will never be nil, so there is no need to
test it.

Unfortunately, the Name property of a form is not available at run
time.  If that is what you are trying to accomplish, then use the type
of the form, instead of its name.  Its Name property is the same as
its type, but without the initial "T".

procedure TForm1.ButtonClick(Sender: TObject);
begin
  Button2.Caption := Copy(ClassName, 2, 255);
end;
--
Ray Lischner         (li...@tempest-sw.com)
Tempest Software, Corvallis, Oregon, USA

Other Threads