How does one remove Event Handlers?

  • Thread starter Thread starter Peter Oliphant
  • Start date Start date
P

Peter Oliphant

In general, some controls can have events handlers attached to them,
typically via the += operator. How does one remove an event handler added
this way? Or, just as good for my purposes, how can one remove all event
handlers attached?
 
You could keep an ArrayList of all the delegates you've added to the event
and then remove them one by one using the -= operator.
 
So, if i understand, I'd should do something like this (code inside a class,
hence the 'this' reference):

EventHandler* handler = new EventHandler( this, m_Handler ) ; //
this->m_Handler is of proper form

timer->Tick += handler ;

and then remove it via:

timer->Tick -= handler ; // this 'handler' is the same variable as above

Is this correct?

Nishant Sivakumar said:
You could keep an ArrayList of all the delegates you've added to the event
and then remove them one by one using the -= operator.

--
Regards,
Nish [VC++ MVP]


Peter Oliphant said:
In general, some controls can have events handlers attached to them,
typically via the += operator. How does one remove an event handler added
this way? Or, just as good for my purposes, how can one remove all event
handlers attached?
 
I guess there is no need to keep an ArrayList of all the delegates you've
added. You can use MulticastDelegate::GetInvocationList to get an array of
all single cast delegates.

Marcus

Nishant Sivakumar said:
You could keep an ArrayList of all the delegates you've added to the event
and then remove them one by one using the -= operator.

--
Regards,
Nish [VC++ MVP]


Peter Oliphant said:
In general, some controls can have events handlers attached to them,
typically via the += operator. How does one remove an event handler added
this way? Or, just as good for my purposes, how can one remove all event
handlers attached?
 
Back
Top