I got this piece for moving an element around on a canvas
Private p As Point
Private Sub moveHandler() Handles Me.MouseDown
p = Mouse.GetPosition(Me)
AddHandler canvasRef.MouseMove, AddressOf moveLoop
End Sub
Private Sub moveLoop()
If Mouse.LeftButton = MouseButtonState.Pressed Then
Dim c As Point = Mouse.GetPosition(canvasRef)
Canvas.SetLeft(Me, c.X - p.X)
Canvas.SetTop(Me, c.Y - p.Y)
Else
RemoveHandler canvasRef.MouseMove, AddressOf moveLoop
End If
End Sub
It underlines the removehandler and says something like "The addressof expression has no effect because it requires a relaxed something something, make delegate and remove that instead!"
Makes no sense to me.
That's because the signature of the moveLoop method doesn't match the signature of the MouseEventHandler delegate. Because VB.NET is so lax, it allows you to add it as a handler for the event, by internally creating an anonymous method with the required parameters that calls your handler. But when you try to remove the handler, it doesn't work because the anonymous method created before is no longer accessible...
The easiest fix is to change the signature of your method so that it matches the signature of the delegate:
Private Sub moveLoop(ByVal sender As Object, ByVal e As MouseEventArgs)
Another option is to store a reference to the handler as shown in keyboardP's answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With