Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass an event as a function parameter?

I have a form that has a list of useful procedures that I have created, that I often use in every project. I am adding a procedure that makes it simple to add a click-able image over where would be the TAccessory of a TListBoxItem. The procedure intakes the ListBox currently, but I would also need it to intake which procedure to call for the OnClick Event for the image.. Here is my existing code:

function ListBoxAddClick(ListBox:TListBox{assuming I need to add another parameter here!! but what????}):TListBox;
var
  i       : Integer;
  Box     : TListBox;
  BoxItem : TListBoxItem;
  Click   : TImage;
begin
  i := 0;
  Box := ListBox;
  while i <> Box.Items.Count do begin
    BoxItem := Box.ListItems[0];
    BoxItem.Selectable := False;

    Click := Timage.Create(nil);
    Click.Parent := BoxItem;
    Click.Height := BoxItem.Height;
    Click.Width := 50;
    Click.Align  := TAlignLayout.alRight;
    Click.TouchTargetExpansion.Left := -5;
    Click.TouchTargetExpansion.Bottom := -5;
    Click.TouchTargetExpansion.Right := -5;
    Click.TouchTargetExpansion.Top := -5;
    Click.OnClick := // this is where I need help

    i := +1;
  end;
  Result := Box;
end;

The desired procedure would be defined in the form that is calling this function.

like image 731
ThisGuy Avatar asked Dec 23 '13 17:12

ThisGuy


People also ask

How do you pass an event in a function?

To pass an event object to a function in JavaScript, we can use the addEventListener method. const onClick = (ev) => { ev. preventDefault(); console.

Can we pass function as a parameter in JavaScript?

Passing a function as an argument to the function is quite similar to the passing variable as an argument to the function. so variables can be returned from a function. The below examples describe passing a function as a parameter to another function.

Can function be passed as parameter to another function?

Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions.

How do you pass an element to a onClick function?

To pass this element to JavaScript onclick function and add a class to that clicked element, we can set the onclick attribute to a function that's called with this . to set the onclick attribute to the a elements to call onClick with this . in our JavaScript code. We call element.


1 Answers

Since the OnClick event is of type TNotifyEvent you should define a parameter of that type. Look at this (I hope self-explaining) example:

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
  private
    procedure TheClickEvent(Sender: TObject);
  end;

implementation

procedure ListBoxAddClick(ListBox: TListBox; OnClickMethod: TNotifyEvent);
var
  Image: TImage;
begin
  Image := TImage.Create(nil);
  // here is assigned the passed event method to the OnClick event
  Image.OnClick := OnClickMethod;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // here the TheClickEvent event method is passed
  ListBoxAddClick(ListBox1, TheClickEvent);
end;

procedure TForm1.TheClickEvent(Sender: TObject);
begin
  // do something here
end;
like image 138
TLama Avatar answered Nov 05 '22 07:11

TLama