0

Possible Duplicate:
How do I Unregister ‘anonymous’ event handler

In my c# method i am using

this.Loaded+=(sender,e)=>{ my code }; 

for executing the code on load time. But my issue is that i don't how to un register this chained events. I tried something similar to

this.Loaded-=(sx,ex)=>{}; 

but the event is not un registering. Any one please help me to solve this issue.

Community
  • 1
  • 1
StezPet
  • 2,430
  • 2
  • 27
  • 49

2 Answers2

6

This code creates anonymous type with method that is attached to event so to detach form this event you need to provide the same method not a new one.

so try this:

EventHandler handler = (sender,e)=>{...};
Loaded += handler;
Loaded -= handler;

Thow for attaching to this. event there usually is nice alternative like

override OnLoaded(EventArgs args)
Rafal
  • 12,391
  • 32
  • 54
2

You can not unregister inline event subscription. If you want to have a possibility to unregister, use a usual delegate/handler subscription.

Tigran
  • 61,654
  • 8
  • 86
  • 123