delphi Self.Componentcount得出的数量不包含动态生成的控件数量,为什么

2025-05-13 14:52:42
推荐回答(1个)
回答1:

因为并没有指定持有者

Owner 属性为 nil

constructor TComponent.Create(AOwner: TComponent);
begin
  FComponentStyle := [csInheritable];
  if AOwner <> nil then AOwner.InsertComponent(Self);
end;
with TEdit.Create(nil) do begin

改为
with TEdit.Create(Self) do begin

或者 指定了 Parent

可以通过 Controls 和 ControlCount 来获得