How to simulate an OnDestroy event on a TFrame in Delphi?
How can i simulate an `OnDestroy` event for a `TFrame` in Delphi?


----------

i nievely added a `constructor` and `destructor` to my frame, thinking that is what `TForm` does:

    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;


The problem with this is that by the time my destructor runs, controls on the frame have been destroyed and are no longer valid.

The reason for this is in the containing form's destructor, which it uses to fire an `OnDestroy` event:

    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 Destroy; //--> calls destructor of my frame
       ...
    end;

The destructor of my frame object is being called when the form's destructor runs. Problem with this is that it's too late. The form calls `DestroyWindowHandle`, which asks Windows to destroy the form's window handle. This recursively destroys all child windows - including those on my frame.

So when my frame's `destructor` runs, i attempt to access controls that are no longer in a valid state. 


----------


How can i simulate an `OnDestroy` event for a `TFrame` in Delphi?


See also
--------

- [Simulating OnCreate and OnDestroy for a Frame?][1]
- [How to Implement the OnCreate event for a Delphi TFrame object][2]
- [Embargadero QC#1767: TFrame misses OnCreate, OnDestroy, OnShow][3]


  [1]: http://www.delphipages.com/forum/showthread.php?t=118316
  [2]: http://delphi.about.com/od/delphitips2007/qt/tframe_oncreate.htm
  [3]: http://qc.embarcadero.com/wc/qcmain.aspx?d=1767
How to simulate an OnDestroy event on a TFrame in Delphi?
===

How can i simulate an `OnDestroy` event for a `TFrame` in Delphi?


----------

i nievely added a `constructor` and `destructor` to my frame, thinking that is what `TForm` does:

    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;


The problem with this is that by the time my destructor runs, controls on the frame have been destroyed and are no longer valid.

The reason for this is in the containing form's destructor, which it uses to fire an `OnDestroy` event:

    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 Destroy; //--> calls destructor of my frame
       ...
    end;

The destructor of my frame object is being called when the form's destructor runs. Problem with this is that it's too late. The form calls `DestroyWindowHandle`, which asks Windows to destroy the form's window handle. This recursively destroys all child windows - including those on my frame.

So when my frame's `destructor` runs, i attempt to access controls that are no longer in a valid state. 


----------


How can i simulate an `OnDestroy` event for a `TFrame` in Delphi?


See also
--------

- [Simulating OnCreate and OnDestroy for a Frame?][1]
- [How to Implement the OnCreate event for a Delphi TFrame object][2]
- [Embargadero QC#1767: TFrame misses OnCreate, OnDestroy, OnShow][3]


  [1]: http://www.delphipages.com/forum/showthread.php?t=118316
  [2]: http://delphi.about.com/od/delphitips2007/qt/tframe_oncreate.htm
  [3]: http://qc.embarcadero.com/wc/qcmain.aspx?d=1767