How to pass an event as a parameter to a function?

I have a form that contains a list of useful procedures that I created that I often use in every project. I am adding a procedure that makes it easy to add a clickable image where TAccessory TListBoxItem will be. The procedure is currently starting the ListBox, but I will also need it to accept which procedure raises 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 required procedure will be defined in the form calling this function.

+4
source share
1 answer

OnClick TNotifyEvent . ( , ) :

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;
+6

All Articles