Jump to content
dummzeuch

wtf is TForm.Action for?

Recommended Posts

Can somebody explain to me what TForm.Action is for? And how it can be used?

 

The OLH is here, but it does not specifically talk about TForm:

 

http://docwiki.embarcadero.com/Libraries/Rio/en/Vcl.Forms.TForm.Action

 

It refers to an example

 

http://docwiki.embarcadero.com/CodeExamples/Rio/en/TFormAction_(Delphi)

 

which I also don't understand.

 

From the example I gather that TForm.Action is not assigned by default, but when and by which mechanism is it executed?

 

 

It's not that I need it, I just came across this property for the first time (it has existed for over a decade).

Share this post


Link to post

I don't ever use the OnExecute handler, but I generally use the OnUpdate handler to update controls that does not support actions themselves.

I guess one could also use it to control TForm.Enabled but I've never had a need for that.

 

The forms OnUpdate handler is also convenient when modernizing applications that were written without TActionList. These tend to have a single method that contains all the UI update logic. I move this logic into the OnUpdate handler, review and remove all the calls to the old update method and then start migrating the logic to individual actions. I mostly end up with some logic that can't be handled by actions and so it stays on the forms OnUpdate handler.

  • Like 1

Share this post


Link to post

"Vcl.Forms.TForm.Action inherits from Vcl.Controls.TControl.Action"

It's for buttons and menu items. You select an action defined in a TActionList object (centralized actions).

It's strange to have this for TForm but this is like in FMX where any control can be a container for any control.

Share this post


Link to post
1 minute ago, Cristian Peța said:

It's for buttons and menu items.

I think the question (and my answer) is specifically about what TForm.Action is for. Not what TControl.Action in general is for.

 

The Action property is explicitly published by TForm so it's not by accident that it's visible in the property inspector.

  • Like 1

Share this post


Link to post

I see that when you click on a form their Action is executed. Action property was probably published for someone that will find something useful in this.

Share this post


Link to post
2 hours ago, dummzeuch said:

came across this property for the first time

  • create an action aiOnIdle using the forms caption
  • link it to the form
  • name the update event OnIdle

Then use that event as needed, beware the OnIdle event won't fire when a modal dialog is up.
Unlike the TActionList.OnUpdate event this one will fire as long as the form is enabled.

 

Share this post


Link to post
Guest
6 hours ago, Cristian Peța said:

Action property was probably published for someone that will find something useful in this.

I agree! So the question still remains. Who would want this, in what context? What is the benefit compared to a much more legible "OnClick" implementation?

Share this post


Link to post
51 minutes ago, Dany Marmur said:

I agree! So the question still remains. Who would want this, in what context? What is the benefit compared to a much more legible "OnClick" implementation?

The same could be asked for any UI control that has a published Action property and a published OnClick (or other actionable) event.  One reason is code reuse, the same Action object can be assigned to multiple controls, even the Form itself.   For instance, Actions are commonly shared with menu items and other UI controls that invoke the same actions.  Another use would be sharing common properties with multiple controls (Enabled, Visible, etc) so you can update 1 property and have it propagate to multiple controls automatically.

  • Like 2

Share this post


Link to post

So, the Action is executed when the user clicks on the form?

Edited by dummzeuch

Share this post


Link to post
Guest
14 hours ago, Remy Lebeau said:

Another use would be sharing common properties with multiple controls (Enabled, Visible, etc) so you can update 1 property and have it propagate to multiple controls automatically.

A point indeed.

Share this post


Link to post

Another advantage of actions and action lists is that they can reside on a datamodule, which can be used in a VCL and a FMX application. Thus the same action logic can be used, which might have to be duplicated otherwise.

  • Like 2

Share this post


Link to post

The same quick question once occurred to me, I concluded I don't have a use for it, and I just ignore it :)

Share this post


Link to post

I'm not questioning the advantages of actions in general, just that of an action property in TForm. I still haven't understood what that particular one can be used for.

Share this post


Link to post
44 minutes ago, dummzeuch said:

I'm not questioning the advantages of actions in general, just that of an action property in TForm. I still haven't understood what that particular one can be used for.

I don't understand what is confusing about it.  Actions have several properties and events that apply to Forms just as they do to any other UI control.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×