function TComponent.FindComponent(const AName: string): TComponent;
var
I: Integer;
begin
if (AName <> '') and (FComponents <> nil) then
for I := 0 to FComponents.Count - 1 do
begin
Result := FComponents[I];
if SameText(Result.FName, AName) then Exit;
end;
Result := nil;
end;
--是在拥有的控件里面找
Owner属性是指构件的所有者,它负责构件的创建和释放。如在上例中,系统默认窗体上所有构件的所有者是窗体,而窗体的所有者是Application。顺便指出,create
方法应带有表示构件所有者的参数,如在上例中,构件所有者是窗体,即self。
//输入对应的组件的名称,如果找到就返回组件的引用,没有找到就返回nil
例子:
var
s: TComponent;
a: string;
begin
a := InputBox('输入框', '请输入要查找的组件', 'aa');
s := FindComponent(a);
if s <> nil then
ShowMessage(s.ClassName)
else
ShowMessage('没有找到,笨死了!');
end;