Loaded procedure executed twice for TFrame's descendant

穿精又带淫゛_ 提交于 2021-02-10 08:10:59

问题


I've registered as component a TFrame's descendant class and I've noticed that the Loaded procedure is executed twice:

Runtime:

  TMyFrame = class(TFrame)
  private
  protected
    procedure Loaded(); override;
  public
    constructor Create(AOwner : TComponent); override;
  end;

constructor TMyFrame.Create(AOwner : TComponent);
begin
  ShowMessage('Before TMyFrame.Create');
  inherited Create(AOwner);
  ShowMessage('After TMyFrame.Create');
end;

procedure TMyFrame.Loaded();
begin
  ShowMessage('Before TMyFrame.Loaded');
  inherited;
  ShowMessage('After TMyFrame.Loaded');
end;

Designtime:

procedure Register;
begin
  RegisterComponents('Test', [TMyFrame]);
end;

Output:

At runtime, on creating a form in which there is a TMyFrame component.

Before TMyFrame.Create

Before TMyFrame.Loaded

After TMyFrame.Loaded

After TMyFrame.Create

Before TMyFrame.Loaded

After TMyFrame.Loaded

At designtime, on adding a TMyFrame to a form:

Before TMyFrame.Create

Before TMyFrame.Loaded

After TMyFrame.Loaded

After TMyFrame.Create

At designtime, opening a form in which there is a TMyFrame component:

Before TMyFrame.Create

Before TMyFrame.Loaded

After TMyFrame.Loaded

After TMyFrame.Create

Before TMyFrame.Loaded

After TMyFrame.Loaded

Why the Loaded procedure is executed twice? How to avoid this?


回答1:


This behavior is as-designed.

Loaded

Loaded may be called multiple times on inherited forms. It is called every time a level of inheritance is streamed in. Do not allocate memory in an overridden Loaded method without first checking that the memory has not been allocated in a previous call.



来源:https://stackoverflow.com/questions/44515417/loaded-procedure-executed-twice-for-tframes-descendant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!