How To…Reassign Event Handlers

Question

“How do I reassign event handlers?”

Solution

Whenever you save a template to an .rtm file, all of the published properties of the Report are saved. This includes all Properties and Events that are visible in the Object Inspector. For the events, the ‘name’ of the event-handler procedure is saved.

When you load an .rtm all of the properties values of the TppReport component are set to the values that were stored to the .rtm.

If you load an .rtm that had OnStartPage assigned then it will be re-attached – provided that the event-handler method is a published procedure of the report’s owner. The Owner is normally the form.

On the other hand, if you load an .rtm that did NOT have OnStartPage assigned when it was saved, you will need to programmatically assign the event-handler.

You have a form with an event-handler for OnStartPage…

type
  Form1 = class(TForm)
    procedure Form1StartPageEvent(Sender: TObject);

  end;

You need to attach the event-handler after loading:

myReport.Template.LoadFromFile;
myReport.OnStartPage := Form1StartPageEvent;