Pass event as parameter of method

Hello,
I have simple object like this:

  THCXBarManagerServices=class
  published
   function CreateUserButton(aBar:integer; aCaption:string; aIconIndex:integer; aOnClick:TNotifyEvent):boolean;
  end;

and

function THCXBarManagerServices.CreateUserButton(aBar: integer; aCaption: string; aIconIndex: integer; aOnClick: TNotifyEvent): boolean;
begin
//some code here

  Item.OnClick:=aOnClick;

//some code here
end;

How to pass aOnCLick event from script. Script looks like:

procedure TestClick(Sender:TObject);
begin
//click
end;                    

procedure InitForm;  
var BS:THCXBarManagerServices;
begin                                   
//some code here

 r:=BS.CreateUserButton(userbar,'Test',12,'TestClick');  

//some code herr
end;

Thank you
Zdenek

Unfortunately that is not possible, directly. I suggest you make the OnClick property of the Item class public, and set it directly from the script.

I agree that property is much better for this purpose. Another way I have tested is EventBroker.SetEvent. I declare event as string and in machine proc I have somethink like this:

procedure TatdxBarLibrary.__THCXBarManagerServicesCreateUserButton(AMachine: TatVirtualMachine);
var
  AResult: variant;
  ALink:TdxBarItemLink;
begin
  with AMachine do
  begin
   ALink := THCXBarManagerServices(CurrentObject).IntCreateUserButton(VarToInteger(GetInputArg(0)),VarToStr(GetInputArg(1)),VarToInteger(GetInputArg(2)));
   if ALink<>nil then begin
    if Scripter.EventBroker.SetEvent(ALink.Item,'OnClick',VarToStr(GetInputArg(3)),Scripter,epReplaceCall)<>nil then
     AResult:=true
    else begin
     Alink.Item.Free;
     AResult:=false;
    end;
   end else
    AResult:=false;
   ReturnOutputArg(AResult);
  end;
end;

DefineMethod is

  With Scripter.DefineClass(THCXBarManagerServices) do
  begin
    DefineMethod('CreateUserButton',4,tkVariant,nil,__THCXBarManagerServicesCreateUserButton,false,0,'aBar: integer; aCaption: string; aIconIndex: integer; aOnClick: string');
  end;

It works, but I have to be sure that this is good approach also. Can scripter work ok with this solution?

Thank you
Zdenek

Yes, your approach is good, actually is exactly the custom implementation needed in case you really wanted to keep the original method signature.

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.