Setting TAction.OnExecute does not enable the button (and menu) lazarus / 31-LCL ---------------------------------------------------------------------- daniel_sap · 2024-10-18 07:03 Hi I usually create the buttons and menus and actions and connect buttons and menus to actions. Later I assign the action methods to Action.OnExecute This typically enables the button and User can click on it. But it's not working any more like this. Do you have some idea if this is a bug or intended behavior I saw a recent change in controlactionlink.inc Quoteprocedure TControlActionLink.SetOnExecute(Value: TNotifyEvent); begin   if IsOnExecuteLinked then FClient.OnClick := Value;   // Note: formerly this changed FClient.OnClick, but that is unneeded, because   //       TControl.Click executes Action end; I have the issue on Mac - Lazarus 3.99 (rev main_3_99-2828-g69e4861d80) FPC 3.2.2 x86_64-darwin-cocoa I thought its FPC 3.3 but it shows 3.2.2 I have it also in windows Lazarus 3.6 (rev lazarus_3_6) FPC 3.2.2 x86_64-win64-win32/win64 I played with it in this test project Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit MainForm; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, ActnList,  StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Action1: TAction;    ActionList1: TActionList;    Button1: TButton;    Button2: TButton;    procedure Button1Click(Sender: TObject);  private   public    procedure Action1Execute(Sender: TObject);   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  Action1.OnExecute := @Action1Execute; // This should enable the action and the button   // Try to set theh action again to check if it works//  Button2.Action := nil;//  Button2.Action := Action1;   // Try to enable the action again. And this works//  Action1.Enabled:=false;//  Action1.Enabled:=true;end; procedure TForm1.Action1Execute(Sender: TObject);begin  ShowMessage('Action');end; end. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i object Form1: TForm1  Left = 318  Height = 240  Top = 130  Width = 320  Caption = 'Form1'  ClientHeight = 240  ClientWidth = 320  LCLVersion = '3.6.0.0'  object Button1: TButton    Left = 64    Height = 25    Top = 120    Width = 80    Caption = 'Button1'    TabOrder = 0    OnClick = Button1Click  end  object Button2: TButton    Left = 184    Height = 25    Top = 120    Width = 80    Action = Action1    TabOrder = 1  end  object ActionList1: TActionList    Left = 152    Top = 56    object Action1: TAction      Caption = 'Action1'    end  endend ---------------------------------------------------------------------- rvk · 2024-10-18 08:43 I'm not sure if this is a bug. But in the source, the only thing I could find was this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TApplication.DispatchAction(Msg: Longint; Action: TBasicAction): Boolean;var  Form: TCustomForm;begin  Form := Screen.ActiveForm;  Result := ((Form <> nil) and (Form.Perform(Msg, 0, PtrInt(Action)) = 1)) or            ((MainForm <> Form) and (MainForm <> nil) and (MainForm.Perform(Msg, 0, PtrInt(Action)) = 1));  // Disable action if no "user" handler is available  if (not Result) and (Action is TCustomAction) and     TCustomAction(Action).Enabled and TCustomAction(Action).DisableIfNoHandler then    TCustomAction(Action).Enabled := Assigned(Action.OnExecute);end; There you see that in that IF statement, TAction.Enabled only gets re- enabled if TAction.Enabled was already true. But once disabled... it never gets re-enabled again (regardless if OnExecute is added). So a simple Enabled := true after setting OnExecute will work: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   Action1.OnExecute := @Action1Execute; // This should enable the action and the button  Action1.Enabled:=true; You can argue that this should be done automatically... but what if you want to assign OnExecute later on but KEEP enabled on false. Was this working in previous versions??? (BTW. In Delphi this works the same way.) ---------------------------------------------------------------------- wp · 2024-10-18 09:27 I don't get what you want to achieve with this weird way of working with actions. Normally I add a TActionList, a TMenu, a TToolbar, some TPopupMenus etc. I add actions and, after doubleclicking on each action, I write a handler for each OnExecute event. Then the actions are assigned to the menu items and tool buttons etc, wherever needed. Works 100%. Why is there a need to assign an OnExecute handler manually? ---------------------------------------------------------------------- rvk · 2024-10-18 10:01 Quote from: wp on October 18, 2024, 11:27:34 amWhy is there a need to assign an OnExecute handler manually? I can imagine assigning a different Action to a TButton at runtime. But in that case you create multiple actions, for each OnExecute each own action. Then it's clear what you assigning in code to the TButton. ---------------------------------------------------------------------- daniel_sap · 2024-10-18 12:51 QuoteSo a simple Enabled := true after setting OnExecute will work: Yes, that's good enough for me. I prefer it like this than automatically setting the Action.Enabled to True. As you mentioned here QuoteYou can argue that this should be done automatically... but what if you want to assign OnExecute later on but KEEP enabled on false. I think my confusion started from this, that during design time Action.Enabled was true and during runtime it was False. (but it makes sense) QuoteWas this working in previous versions??? I thought it was working in previous versions, but now after discussing I have doubts - I'm checking changes in my code which could lead to this. QuoteWhy is there a need to assign an OnExecute handler manually? I have ready code in different packages, which I assign directly to actions. Also, I wanted to separate the code in different places and just assign the proper methods from different places. (There are hundreds of actions in the project and dozens in packages). Actually I plan to move actions creations on different places, depending on the features, but still considering all aspects. Also, I have some actions which created at runtime.