C# how to remove delegation [duplicate]
Possible Duplicate:
Unsubscribe anonymous method in C#
I have some Panels that when created they are delegated to become clickable:
int z2 = 开发者_运维知识库z;
PicBx[z].Click += delegate { clicked(z2, null); };
I want to be able to remove this if the program calls for it. I tried using:
int z2 = z;
PicBx[z].Click -= delegate { clicked(z2, null); };
But it didn't work. Is there any way to remove the clickable delegation from it?
You can't unsubscribe from anonymous delegate, you need to keep reference to delegate
var myClickDelegate = (RoutedEventHandler)delegate { clicked(z2, null); };
PicBx[z].Click += myClickDelegate;
...
PicBx[z].Click -= myClickDelegate;
Or create named function
Hope this helps
精彩评论