I am wondering whether there is a better solution to send local variables to an Action defined in another unit then the following:
I am currently trying to share a set of actions across a number of different forms.
The difficulty I face is to send the relevant data to the action: in my situation I want to ensure that the action receives the value of the current row in a particular dataset and the action will take place on that value.
I have come up with the following method which is to create an event in the datamodule that is then assigned to a procedure in the calling form, in the following manner:
Data module holding actions:
unit actions.contacts;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, ActnList
, Dialogs;
type
TActionParameter = procedure(var aStrParam:string) of object;
{ Tcln_actions }
Tcln_actions = class(TDataModule)
Trial: TAction;
ActionList1: TActionList;
procedure TrialExecute(Sender: TObject);
private
public
OnGetActionParameter : TActionParameter;
Function GetClientNumber : string;
end;
var
cln_actions: Tcln_actions;
implementation
{$R *.lfm}
{ Tcln_actions }
procedure Tcln_actions.TrialExecute(Sender: TObject);
begin
showMessage(GetClientNumber);
end;
function Tcln_actions.GetClientNumber: string;
var x : string;
begin
x := '';
if Assigned(OnGetActionParameter) then // tests if the event is assigned
begin
OnGetActionParameter(x); // calls the event.
end;
result := x;
end;
end.
The main form will then assign a local procedure to 'OnGetActionParameter' so as to define the data to be sent to the action. While the action 'cln_actions.Trial' is assigned to whichever gui element necessary (eg a menu item).
uses
..., actions.contacts, ...
Type
...
procedure SendParameter(var Something:string);
<Create Form or Show Form>
cln_actions.OnGetActionParameter:= @SendParameter;
...
procedure TForm.SendParameter(var Something: string);
begin
Something := 'hello there!';
end;
And ideas would be helpful