When you create a delegate pointing to an instance method, you have to provide the "
Me
" object which will be used when the delegate is invoked. Because you haven't done that, and you've specified
False
for the
throwOnBindFailure
parameter, your delegate instance will be set to
Nothing
.
There are two ways you can solve this:
1. To create a delegate bound to a single instance of the class, use one of the
CreateDelegate
overloads which accept a
firstArgument
parameter:
Dim myDelegate As delegate_OnClick = [Delegate].CreateDelegate(
GetType(delegate_OnClick),
Repeat_Tastendruck_Control,
myInfo)
myDelegate(EventArgs.Empty)
Delegate.CreateDelegate Method (Type, Object, MethodInfo) (System)[
^]
2. To create a delegate which can be reused with multiple instances of the class, give your delegate an extra parameter to represent the object instance:
Private Delegate Sub delegate_OnClick(ByVal instance As YourControlType, ByVal e As EventArgs)
...
Dim myDelegate As delegate_OnClick = [Delegate].CreateDelegate(GetType(delegate_OnClick), myInfo, True)
myDelegate(Repeat_Tastendruck_Control, EventArgs.Empty)
NB: The type of the first parameter for the delegate must be the type where the method is declared, or a derived type. If you want to pass an interface, then the
MethodInfo
must come from the interface, not the class.