var
AButton:TButton;
if FindChildControl('button1')<>nil then
AButton:=FindChildControl('button1') as TButton; //用as强制
with AButton do begin
caption:='XXX';
onClick:=myClick;
....
end;
AButton:TButton;
if FindChildControl('button1')<>nil then
AButton:=FindChildControl('button1') as TButton; //用as强制
with AButton do begin
caption:='XXX';
onClick:=myClick;
....
end;
n:TEdit;
begin
n:=(button2.FindComponent('edit5') as TEdit);
if n is TEdit then
showmessage('love');
end;
to:txinfo(冰炎两重天),上面的代码有错吗?
是指查找以它本身为owner的组件
你上面那个edit5的owner只能是TForm类型,不可能是Button2
所以findcompoent('edit5')的返回结果只能是nil
这时再用as 强制转换,肯定会报错
把button2.去掉应该就可以
还有if n is TEdit这句话不需要了,因为你前面已经声明过它是一个TEdit类型变量