0

I dynamically add and remove TextChanged event:

TextBox.TextChanged -= new System.EventHandler(this.textBox_TextChanged);
........
TextBox.TextChanged += new System.EventHandler(this.textBox_TextChanged);

How can I know at some moment if this event was attached or detached?

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
lit17
  • 37
  • 1
  • 5
  • 3
    There is no way to do that. – SLaks Jan 22 '15 at 20:54
  • 6
    you should wrap the add/remove in a function - then you can see when those are called. – Daniel A. White Jan 22 '15 at 20:54
  • An event event notification system. That meta. – ahoffer Jan 22 '15 at 20:56
  • 1
    One of the arguments why people like to use Rx ... :) – walther Jan 22 '15 at 21:14
  • possible duplicate of [Has an event handler already been added?](http://stackoverflow.com/questions/136975/has-an-event-handler-already-been-added) Also [this](http://stackoverflow.com/questions/937181/c-sharp-pattern-to-prevent-an-event-handler-hooked-twice) but otoh also [this](http://stackoverflow.com/questions/2697247/how-to-determine-if-an-event-is-already-subscribed) – TaW Jan 22 '15 at 21:25

1 Answers1

0

There is no way to determine if an event was ever attached or removed, particularly not from an event in a class you didn't define. An event defined in another class can only appear on the left side of an add/remove operation - attempting to do otherwise will result in the compiler telling you as much as an error.

If you want to know about this for some reason that can't be better suited otherwise, consider instead raising a flag when attaching this even handler, so that you can query said flag later.

David
  • 10,458
  • 1
  • 28
  • 40