1

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

let ButtonClicked (evArgs)= 
     DoSomthing...

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

代码有效但我想要这样的东西

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

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

谢谢你的帮助。

4

2 回答 2

2

你可以这样做:

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

button.Click.Add(fun evArgs -> ButtonClicked evArgs button)
于 2014-03-13T08:08:39.823 回答
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)
;;

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

于 2014-03-13T08:30:04.577 回答