-1

I want to call a method when an animation ends. I'm using this statement:

AddHandler anim.Completed, AddressOf anim_completed

But i want the anim_completed sub to be with parameters. Any ideas how to do that?

dreameral
  • 121
  • 1
  • 9
  • 1
    Just add parameters to the sub...i don't get it. Also, you're method names need work. Upper camel case. – rory.ap Aug 22 '16 at 14:32
  • Why would you think i didn't try that? Anyway if i add parameters to the sub i get this:'Public Sub anim_completed(target As Ellipse)' does not have a signature compatible with delegate 'Delegate Sub EventHandler(sender As Object, e As EventArgs)'. – dreameral Aug 22 '16 at 14:37
  • [Completed](https://msdn.microsoft.com/en-us/library/system.windows.media.animation.timeline.completed(v=vs.110).aspx) is an event that takes a handler method with a fixed signature. You can't change that. – Clemens Aug 22 '16 at 14:37
  • So i can't call a method with parameters when an event occurs? – dreameral Aug 22 '16 at 14:38
  • @Eae --, well, I would think you didn't try that because you didn't say so. Why didn't you include more pertinent information in your question like "if i add parameters to the sub i get this:'Public Sub anim_completed(target As Ellipse)' does not have a signature compatible with delegate 'Delegate Sub EventHandler(sender As Object, e As EventArgs)'"?? – rory.ap Aug 22 '16 at 14:38
  • You're right, my bad :) – dreameral Aug 22 '16 at 14:39

2 Answers2

1

Simple solution. Use Anonymous Event Handler, then you will be able to access Ellipse object directly.

Dim target As Ellipse = Nothing

AddHandler anim.Completed, Sub()
                                If target IsNot Nothing Then

                                End If
                            End Sub
Anton Kedrov
  • 1,767
  • 2
  • 13
  • 20
  • `Use Anonymous Event Handler` this is asking for issues when not `Removing` them... There is a thing called `WithEvents` that handles all of this for you, adding and removing handlers... – Trevor Aug 22 '16 at 15:27
  • Do you need to? :) If you find that you do, then you can find answers here: http://stackoverflow.com/questions/1362204/how-to-remove-a-lambda-event-handler – Anton Kedrov Aug 22 '16 at 15:34
0

Your delegate/method need to have the same signature as the event. But inside that event you can call what ever you want.

AddHandler anim.Completed, AddressOf onAnimCompleted

Sub onAnimCompleted(sender As Object, e As EventArgs)

    Dim target As New Ellipse

    anim_completed(target)

End Sub
the_lotus
  • 12,668
  • 3
  • 36
  • 53