tags:

views:

43

answers:

2

I have a windows form with a button on it and I only want that button to be visible, if the form that calls it, has subscribed to one of the forms custom events.

I know I can check whether myEvent == null, but what if the event is subscribed to after the form has been loaded?

Is this possible?

(Perhaps just a timer on the form, continually checking whether the event is null?? (sounds messy though)

+2  A: 

Just like properties events have associated methods as well so:

public event EventHandler MyEvent {
    add {
        // do something
    }
    remove {
        // do something 
    }
}
Qberticus
+4  A: 

If it's a custom event, you can put the overrides on where the client subscribes / unsubscribes:

   private EventHandler _customEventDelegate;

   public event EventHandler MyCustomEvent
   {
      add
      {
          bool wasNull = (_customEventDelegate == null);
          _customEventDelegate += value;
          if(wasNull)
          {
              this.ChangeButtonVisibility(true);
          }
      }
      remove
      {
          _customEventDelegate -= value;
          if(_customEventDelegate == null)
          {
             this.ChangeButtonVisibility(false);
          }
      }
   }
Andrew Shepherd