MilkRoad和l_x_yuan的办法在大多数情况下管用,
但当所要找的控件的Owner不是Form1时就失效了.
可以改进一下:
function Find(Owner:TComponent; ComName:String):TComponent;
var
i:Integer;
begin
for i:=0 to Owner.ComponentCount-1 do begin
Result:=Owner.Components;
if Result.Name=ComName then exit;
Result:=Find(Result,ComName);
if Result<>nil then exit;
end;
Result:=nil;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Button:TButton;
begin
Button:=TButton.Create(Button1);//临时产生一个Button
Button.Name:='TestBtn';
if Find(Self,'TestBtn') is TWinControl //可以找到
then showMessage('ok');
if FindComponent('TestBtn')<>nil //这个找不到的
then ShowMessage('I find too.');
Button.Free;
end;