Understanding AddHandler and passing delegates and events

I use AddHandler to hook a function to a control event that I dynamically create:

Public Delegate Sub MyEventHandlerDelegate(ByVal sender As Object, ByVal e As System.EventArgs)

Public Sub BuildControl(EventHandler as System.Delegate)

         dim objMyButton as new button

         AddHandler objMyButton.Click, EventHandler

    end Sub

This code throws a runtime exception:

Unable to pass an object of type "MyEventHandlerDelegate" to enter "System.EventHandler"

What I don’t understand about System.Delegate, although AddHandler accepts an argument of type "System.Delegate"? What type of "EventHandler" should be applied to the type that AddHandler can accept? Thank you for your help!

+3
source share
2 answers

, , . click System.EventHandler. , , System.EventHandler, System.EventHandler.

, : System.EventHandler System.Delegate:

Public Sub BuildControl(EventHandler as System.EventHandler)
     dim objMyButton as new button
     AddHandler objMyButton.Click, EventHandler
End Sub
+5

, BuildControl, MyEventHandlerDelegate. ? , , Button.Click.

0

Source: https://habr.com/ru/post/1737428/


All Articles