1

HI

I have an application with more than one frame that act as forms.

I have temporary files I need to delete when the user moves from a certain frame and I currently delete the file when they press OK or CANCEL.

If they just close the application I also want to delete the temporary files but cannot determine when the frame is destroyed.

Regards, Pieter

Pieter van Wyk
  • 2,316
  • 9
  • 48
  • 65
  • 1
    Possible duplicate: http://stackoverflow.com/questions/3979298/how-to-simulate-an-ondestroy-event-on-a-tframe-in-delphi – Sertac Akyuz Jan 28 '11 at 17:17
  • I don't really understand your question. If you want to take an action when the application closes, you have plenty of options, most commonly you would attach something to the MainForm's destructor, perhaps using it's OnDestroy event. And if you really want something to run when a frame is destroyed, then you just override its detructor as Rob says. But that all seems so basic, that perhaps there is something more to your question that we can't discern yet. – David Heffernan Jan 28 '11 at 17:24
  • Sorry, my question is not that clear, but the answer below would definately solve my problem. I need to delete a file when the frame is destroyed. Thanks. Pieter. – Pieter van Wyk Jan 31 '11 at 08:52

1 Answers1

7

To detect when a frame is destroyed, override its destructor, Destroy.

If overriding the destructor is not appropriate (say, if the files belong to the enclosing form rather than the frame), then you can use component notification. After you create the frame, call its FreeNotification method, passing in a reference to the enclosing form. When the frame is destroyed, it will call the form's Notification method. Override that method in your form, and if the component is the frame, delete the files. (That method may be called many times in your program, so checking the AComponent parameter is important.)

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467