Inheritance 在继承窗口之后,应在何处单击事件添加?

Inheritance 在继承窗口之后,应在何处单击事件添加?,inheritance,f#,Inheritance,F#,下面的测试代码,当打开窗口时,也会弹出hello消息框。听起来,当打开窗口时,它会在fun->之后运行代码。 当我调试查看没有test001时,它似乎不是一个接一个地运行的,比如在fun\uu->之后不运行代码: let test001 = MessageBox.Show("hello") type Server() as this = inherit windows do connectionButton.Click.Add (fun _ -> test001

下面的测试代码,当打开窗口时,也会弹出hello消息框。听起来,当打开窗口时,它会在
fun->
之后运行代码。 当我调试查看没有test001时,它似乎不是一个接一个地运行的,比如在
fun\uu->
之后不运行代码:

let test001 = MessageBox.Show("hello")
type Server() as this =
    inherit windows
       do connectionButton.Click.Add (fun _ -> test001
                                            tc.Connect("localhost", 2626) )

您需要像这样创建
Server
的实例

假设
Server
继承自
System.Windows.Form

System.Windows.Forms.Application.Run(new Server())

由于
test001
是一个值,因此只对其求值一次。您需要的是一个函数,它将在每次调用MessageBox时弹出一个MessageBox:

let test001() = MessageBox.Show("hello") // test001 is now a function
type Server() as this =
    inherit windows
       do connectionButton.Click.Add (fun _ -> test001() |> ignore
                                               tc.Connect("localhost", 2626))