Recent

Author Topic: [SOLVED] Assigning Action to ToolButton  (Read 696 times)

egsuh

  • Hero Member
  • *****
  • Posts: 712
[SOLVED] Assigning Action to ToolButton
« on: March 30, 2021, 07:50:16 am »
(The title) removes pre-defined Image index and Hint content of TToolButton.
Assigning Onclick event does not.
And the event handler's Sender is TAction, when ToolButton's Action property is defined.

Are these normal?
« Last Edit: March 31, 2021, 07:40:50 am by egsuh »

devEric69

  • Sr. Member
  • ****
  • Posts: 465
Re: Assigning Action to ToolButton
« Reply #1 on: March 30, 2021, 09:52:18 am »
Hello,

Basically, AFAIK, the properties of a TAction should be defined first, and then when a TControl is connected to such an TAction, the common properties among TAction (and its connecting TControl) should diffuse (from TAction towards TControl) and replace those of the connecting TControl. Nevertheless, it's always possible to override those of a connected TAction by rewriting them in their property editor.

This is the normal behavior.
« Last Edit: March 30, 2021, 09:58:03 am by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 501
  • Professional amateur ;-P
Re: Assigning Action to ToolButton
« Reply #2 on: March 30, 2021, 10:33:23 pm »
Hey egsuh,

A TAction is the perfect way to have code placed in only one place and easily assigned to other components.

Lets say, for example, that you have:
  • A menu entry
  • A Toolbar Button
  • A button

And you want all those 3 to have the same code executed upon being chosen/clicked, but apart from that you want them to have the same Hint, ShortCut, Caption, Image, etc...
Well, the best way to do it is by filling all those properties on the TAction. Then when you assign a TAction to your component, all those properties get populated from the TAction.
Now, this doesn't mean that the components is now locked to the TAction, it just means that all properties have been copied to it.
If you want to have them be different from the TAction, just fill them out after you assign the TAction.

Hope that helps!

Cheers,
Gus
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

egsuh

  • Hero Member
  • *****
  • Posts: 712
Re: Assigning Action to ToolButton
« Reply #3 on: March 31, 2021, 07:40:37 am »
Quote
A TAction is the perfect way to have code placed in only one place and easily assigned to other components.

Yes, I know that. Just problem is I have no way to know from where the call of the action come from. I tried to share the same action between several buttons but behave slightly differently. I needed to let the action's OnExecute procedure know where the call comes from, but once I assign Action property of a button then all the calls were from TAction.  So I directly assigned actMyActionExecute procedure to the button's OnClick events. In this way, I could check the Sender.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 501
  • Professional amateur ;-P
Re: Assigning Action to ToolButton
« Reply #4 on: March 31, 2021, 01:38:21 pm »
Hey egsuh,

Yes, I know that. Just problem is I have no way to know from where the call of the action come from. <snip>

I'm afraid that that's not entirely correct:
Code: Pascal  [Select][+][-]
  1. // Form section
  2. btnSomeAction: TButton;
  3. btnSomeAction1: TButton;
  4. mnuSomeAction: TMenuItem;
  5. tbtnSomeAction: TToolbarButton;
  6.  
  7. procedure TForm1.actSomeActionExecute(Sender: TObject);
  8. begin
  9.   If Sender = btnSomeAction then
  10.   begin
  11.     // This comes from the Button
  12.   end;
  13.   If Sender = btnSomeAction1 then
  14.   begin
  15.     // This comes from the Button 1
  16.   end;
  17.   If Sender = mnuSomeAction then
  18.   begin
  19.     // This comes from the Menu Item
  20.   end;
  21.   If Sender = tbtnSomeAction then
  22.   begin
  23.     // This comes from the Toolbar Button
  24.   end;
  25. end;

Does that help?

Cheers,
Gus
« Last Edit: March 31, 2021, 01:42:06 pm by Gustavo 'Gus' Carreno »
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

egsuh

  • Hero Member
  • *****
  • Posts: 712
Re: [SOLVED] Assigning Action to ToolButton
« Reply #5 on: March 31, 2021, 01:49:04 pm »
Quote
I'm afraid that that's not entirely correct:

I explained that.

If defined as follows, Sender is always TAction.

   btnSomeAction.Action:= actSomeAction;
   btnSomeAction1.Action:= actSomeAction;
   mnuSomeAction.Action:= actSomeAction;
   tbtnSomeAction.Action:= actSomeAction;

But I can discern Sender if I define as follows:

   btnSomeAction.OnClick:= actSomeActionExecute;
   btnSomeAction1.OnClick:= actSomeActionExecute;
   mnuSomeAction.OnClick:= actSomeActionExecute;
   tbtnSomeAction.OnClick:= actSomeActionExecute;


Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 501
  • Professional amateur ;-P
Re: [SOLVED] Assigning Action to ToolButton
« Reply #6 on: March 31, 2021, 02:05:27 pm »
Hey egsuh,

Wow, I'm really sorry, egsuh, for such a patronizing message!!

I was under the impression that the OnExecute would carry the caller's object. I even went and tested it. That's how far I was up my own arse :(

You are completely right, if you want to know who's the caller, the OnExecute of the TAction is completely useless!!

And again, my deepest apologies for being such and arse!!

Cheers,
Gus
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

wp

  • Hero Member
  • *****
  • Posts: 8392
Re: [SOLVED] Assigning Action to ToolButton
« Reply #7 on: March 31, 2021, 02:32:25 pm »
But when you press F1 on "TAction" (or better: Ctrl+Click to open the implementing unit) and then navigate up the inheritance you'll see at the most elemental action, TBasicAction, that there is a public property Actioncomponent (TComponent) for which the help file says: "Return the component that initiates the action."

So, please run at attached demo and see that this is exactly what you are looking for. OnExecute is not useless.

Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 501
  • Professional amateur ;-P
Re: [SOLVED] Assigning Action to ToolButton
« Reply #8 on: March 31, 2021, 02:37:40 pm »
Hey WP,

<snip> TBasicAction, that there is a public property Actioncomponent (TComponent) for which the help file says: "Return the component that initiates the action."

WP, YOU DA MAN!!

That's why I'm the ignorant fool making such generalised false statements. Thanks for the lesson!!

Cheers,
Gus
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

 

TinyPortal © 2005-2018