Delphi 如何将事件作为函数参数传递?

Delphi 如何将事件作为函数参数传递?,delphi,events,parameter-passing,Delphi,Events,Parameter Passing,我有一个表单,其中列出了我创建的有用的过程,我经常在每个项目中使用这些过程。我正在添加一个过程,使添加一个可单击的图像变得简单,该图像位于TListBoxItem的访问位置。该过程当前接收列表框,但我还需要它接收调用图像OnClick事件的过程。。以下是我现有的代码: function ListBoxAddClick(ListBox:TListBox{assuming I need to add another parameter here!! but what????}):TListBox;

我有一个表单,其中列出了我创建的有用的过程,我经常在每个项目中使用这些过程。我正在添加一个过程,使添加一个可单击的图像变得简单,该图像位于TListBoxItem的访问位置。该过程当前接收列表框,但我还需要它接收调用图像OnClick事件的过程。。以下是我现有的代码:

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;
函数ListBoxAddClick(ListBox:TListBox{假设我需要在这里添加另一个参数!!但是什么???):TListBox;
变量
i:整数;
盒子:TListBox;
BoxItem:TListBoxItem;
点击:TImage;
开始
i:=0;
Box:=列表框;
当我开始计算Box.Items.Count时
BoxItem:=Box.ListItems[0];
BoxItem.Selective:=False;
单击:=Timage.Create(无);
Click.Parent:=BoxItem;
单击.Height:=BoxItem.Height;
点击。宽度:=50;
单击.Align:=TAlignLayout.OK;
Click.TouchTargetExpansion.Left:=-5;
Click.TouchTargetExpansion.Bottom:=-5;
Click.TouchTargetExpansion.Right:=-5;
Click.TouchTargetExpansion.Top:=-5;
Click.OnClick:=//这就是我需要帮助的地方
i:=+1;
结束;
结果:=框;
结束;
所需的过程将以调用此函数的形式定义。

由于事件属于该类型,您应该定义该类型的参数。看看这个(我希望是自我解释的)例子:


@MartynA-这是一个循环的代用品@Jordan-通常的做法是使用
for i:=0 to Box.Items.Count-1 do begin/…
,尽管
while
循环也可以工作。对不起,我是一个自学的初学者。lol我的代码比“…”更容易理解。。。。。到箱子。物品。计数1’等。
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;