Toster
Toster

Reputation: 381

How to call FormClose event handler?

I try to call the method FormClose, but I have a problem with its parameters when I try:

FormName.FormClose(nil, CaFree);

Normally I can call a component's event handler with a parameter using nil or sender as TOBject. But now I get the error:

Constant object cannot be passed as var parameter

I have tried many combinations for these two TObject and TAction values. For TObject I tried `sender as TObject', and for TAction all parameters like CaFree etc.

Upvotes: 3

Views: 4571

Answers (1)

David Heffernan
David Heffernan

Reputation: 612983

The second parameter is a var parameter which is what the compiler error message is telling you. So you need to pass a variable. You cannot pass a literal.

var
  Action: TCloseAction;
....
Action := caFree;
Name.FormClose(nil, Action);

Note that you almost certainly should not be doing this. You are not meant to call event handlers directly. The framework will call them at the appropriate time. I think it exceedingly likely that you are mistaken in thinking that you need to fire this event handler directly, or even execute the code outside the normal scenario of the form closing.

As a general rule, if you need to invoke code in an event handler directly then the normal approach is to first extract it to a separate method which can readily be called directly. Then refactor the event handler to call that separate method.

Upvotes: 4

Related Questions