将EventHandler函数添加到按钮单击事件,并将sender作为参数

时间:2014-03-13 07:38:52

标签: f# eventhandler

将EventHandler函数添加到按钮单击事件,并将sender作为参数。

let ButtonClicked (evArgs)= 
     DoSomthing...

button.Click.Add(fun evArgs -> ButtonClicked())

Code Works但是我想要像这样的事情

let ButtonClicked (evArgs,sender)= 
     sender.DoSomthing()
     ...

button.Click.Add(fun evArgs,sender -> ButtonClicked())

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

你可以这样做:

let ButtonClicked evArgs sender =
    // Do something interesting here
    ()

button.Click.Add(fun evArgs -> ButtonClicked evArgs button)

答案 1 :(得分:2)

编辑:完整的例子。将其粘贴到解释器中:

open System.Windows.Forms

let form = new Form(Text = "Form")
let button = new Button(Text = "Button") 
form.Controls.Add(button)

let ButtonClicked sender evargs = 
    printfn "From '%A' received '%A'" sender evargs
button.Click.AddHandler (new System.EventHandler(ButtonClicked))

Application.Run(form)
;;

(顺便说一下,你可以将它粘贴到解释器中有多酷?)