gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:59:23 25 4
gpt4 key购买 nike

我有一个表单,其中包含我创建的有用过程的列表,我经常在每个项目中使用它们。我添加了一个过程,可以轻松地在 TListBoxItem 的 TAccessory 上添加可单击图像。该过程当前接收 ListBox,但我还需要它接收为图像调用 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;

所需的过程将以调用此函数的形式定义。

最佳答案

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;

关于delphi - 如何将事件作为函数参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20748471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com