I think the method you want is called OnMouseMove
.
type MyForm() =
inherit Form()
override this.OnMouseMove(e) = printfn "Mouse is at %d %d" e.X e.Y
Вопрос
How do I override the MouseMove (or any mouse event for that matter) in F#, similar to how it's done in C#? Meaning I want to write something like this, just to get the current mouse coordinates.
override form.MouseMove e =
mouseX = e.X
mouseY = e.Y
Edit: basically I want to access the MouseEventArgs, and in this code the argument e is interpreted as PaintEventArgs instead.
Second Edit: This code actually works, just had another code block with an override on the OnPaint event, so apparently I couldn't use the same event argument variable (e) for both overrides in F#.
Решение 2
I think the method you want is called OnMouseMove
.
type MyForm() =
inherit Form()
override this.OnMouseMove(e) = printfn "Mouse is at %d %d" e.X e.Y
Другие советы
As an alternative to overriding the virtual method in a derived class, you could use a F# object expression to achieve the same result. This is useful to create an instance of overridden behavior without the need to define a derived class.
let mutable mouseLoc = Point()
let myForm = {
new Form() with
override __.OnMouseMove args =
mouseLoc <- args.Location }