我怎样才能在Delphi中模拟一个OnDestroy事件TFrame?
我简单地添加了一个constructor和destructor我的框架,认为这是做什么的TForm:
TframeEditCustomer = class(TFrame)
...
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
...
end;
constructor TframeEditCustomer.Create(AOwner: TComponent)
begin
inherited Create(AOwner);
//allocate stuff
end;
destructor TframeEditCustomer.Destroy;
begin
//cleanup stuff
inherited Destroy;
end;
Run Code Online (Sandbox Code Playgroud)
这个问题是,当我的析构函数运行时,框架上的控件已经被破坏并且不再有效.
原因在于包含表单的析构函数,它用于触发OnDestroy事件:
destructor TCustomForm.Destroy;
begin
...
if OldCreateOrder then DoDestroy; //-->fires Form's OnDestroy event; while controls are still valid
...
if HandleAllocated then DestroyWindowHandle; //-->destroys all controls on the form, and child frames
...
inherited …Run Code Online (Sandbox Code Playgroud)