2013-12-23 27 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

相關問題