"malcolm" <ch********@yahoo.com> wrote in message news:4f**************************@posting.google.c om...
Is there a way to test to see if an event handler hasn't already been
added to an event handler listener object?
public bool IsHandlerAttached( MyEventHandler eh)
{
return ((IList)(MyEvent.GetInvocationList( ))).Contains( eh);
}
However, this functionality must be performed within the class that
exposes MyEvent. Outside of the class, you can't access MyEvent
as if it were a Delegate (which is what it is).
This is what I am doing now as a hack:
dataView.ListChanged -= new
ListChangedEventHandler(dataView_ListChanged);
dataView.ListChanged += new
ListChangedEventHandler(dataView_ListChanged);
Why not just save the EventHandler in a field? Like this,
private ListChangedEventHandler m_notifyListChanged = null;
// . . .
// Attach an event handler.
this.m_notifyListChanged = new ListChangedEventHandler( dataView_ListChanged);
dataView.ListChanged += this.m_notifyListChanged;
// . . .
// Ask yourself if you've attached an event handler.
if ( null != this.m_notifyListChanged )
{
// If so, detach the event handler.
//
dataView.ListChanged -= this.m_notifyListChanged;
this.m_notifyListChanged = null;
}
// . . .
Derek Harmon