Windows窗体中的自定义控件出现问题



我正在尝试用F#编写我自己的MineSaver。

我创建了一种类型的按钮:

type myButton(pos : Point, boum : bool, wnd : Form) = 
inherit Button(Parent = wnd, 
Bounds = Rectangle(Point((pos.X+1) * size,(pos.Y+1) * size), Size(size,size)),
Visible = true,
Text = if boum = true then "B" else "")
let (bomb : bool) = boum
member this.Bomb with get() = bomb 

按钮在表单中,我可以使用GethildAtPoint获得这些按钮。但我有一个控制按钮,而不是myButton,所以我无法访问现场炸弹。

我怎样才能拿到它?我尝试使用Tag属性,但没有成功。

谢谢。

当您调用GetChildAtPoint时,您将返回一个Control,然后您可以用模式匹配对其进行类型测试,看看它是否是您的自定义按钮:

let ctrl = form.GetChildAtPoint pt
match ctrl with
| :? myButton as myb ->
let isBomb = myb.Bomb ()
// Do something here
()
| _ -> () // ignore anything else

最新更新