2013-12-23 62 views
4

我有一个表单,它有我创建的有用过程的列表,我经常在每个项目中使用它。我添加了一个程序,可以简单地在可以点击的图像上添加一个TListBoxItem的TAccessory。该程序引入口列表框当前,但我也将它需要摄入量,以呼吁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; 

所需的程序将在形式定义那就是调用这个函数。

+0

@MartynA - 这是一个ersatz'for'循环。 @Jordan - 通常情况下,使用'for i:= 0到Box.Items.Count - 1开始// ...',尽管while循环也是如此。 –

+0

对不起,我是一个正在自学的初学者。大声笑我的代码比'....... box.items.count-1'更容易理解。 – ThisGuy

回答

6

由于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; 
+0

谢谢。我的道歉,我不知道如何以一种“可传递”的方式提出要求通过什么参数的方式提问。目的。或者即使我可以这样做。 – ThisGuy

+2

不客气!不用担心,这个问题是可以理解的。只是我需要阅读它两次才能理解,因为今天我们已经有了一个小小的庆祝今年年底:-) – TLama

相关问题