应该在继承窗口后点击事件添加?
问题描述:
下面的测试代码,当打开窗口时,它也会弹出hello消息框。这听起来是在打开窗口后运行fun _ ->
后的代码。 当我调试,看不test001,似乎不是一个跑一个,像fun _ ->
后无法运行该代码:应该在继承窗口后点击事件添加?
let test001 = MessageBox.Show("hello")
type Server() as this =
inherit windows
do connectionButton.Click.Add (fun _ -> test001
tc.Connect("localhost", 2626))
答
由于test001
是一个值,它仅被评估一次。你需要的是一个函数,它会在每次调用时弹出一个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))
答
你需要这样的
假设从Server
继承创建Server
实例System.Windows.Form
System.Windows.Forms.Application.Run(new Server())