procedure TForm1.Button1Click(Sender: TObject); var Comp: TComp; begin Comp := FindComponent('Button1'); if (Comp <> nil) and (Comp is TButton) then TButton(Comp).Caption := 'Hello World'; end;
特别提示: 在DLL 中 (Comp as TButton) 可能会报类转换错误, 但是 TButton(Comp)可能就不会. 在DLL中最好与不要用is 进行类型检测, 用if Comp.ClassName = TButton.ClassName then 比 if Comp is TButton then更好些.
begin
(FindChildControl( 'Button1' ) as TButton ).Caption:='hello world';
end;
假如给控件名叫edit1的文本框赋值
TEdit(FindComponent('edit1')).Text:='赋值';
procedure TForm1.Button1Click(Sender: TObject);
var
Comp: TComp;
begin
Comp := FindComponent('Button1');
if (Comp <> nil) and (Comp is TButton) then
TButton(Comp).Caption := 'Hello World';
end;