Problem with a custom Control in Windows Form

Viewed 36

I'm trying to write my own MineSweeper in F#.

I create a type of Button :

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 

The buttons are in a Form and I can get these buttons with GethildAtPoint. But I get a Control not a myButton so I can't access to the the field Bomb.

How can I get it ? I tried with the Tag property, but without success.

Thank you.

1 Answers

When you call GetChildAtPoint, you will get back a Control, which you can then type test with a pattern match to see if it's your custom button:

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