views:

89

answers:

2

I have a custom datagridviewcolumn in which i've added an event. The problem is, I can't work out how to see who has subscribed to the current column object's event and add those subscriptions to the cloned column object.

I get around this problem by asking the calling program to pass the address of the handler to a delegate in the custom column, instead of adding a handler to the event.

Forgive my terminology, I hope you understand what i'm trying to say!

By receiving the reference to the method, the datagridviewcolumn now has control and can then easily clone that reference.

This is fine, but users of controls expect to be able to suscribe to events by selecting the event in visual studio - which creates a template of the method.

+1  A: 

At least in C# you can have "adders" and "removers" for events, like getters and setters for properties.

Maybe you can use that to do some custom processing during the process of somebody adding an event handler to the event?

EDIT
I don't know much about VB.NET, but I googled a little and found the following snippet:

Public Delegate Sub WorkDone(ByVal completedWork As Integer ) 
Private handlers As New ArrayList() 

Public Custom Event WorkCompleted As WorkDone 
  AddHandler (ByVal value As WorkDone)
    If handlers.Count <= 5 Then 
      handlers.Add(value)  
    End If  
  End AddHandler 

  RemoveHandler(ByVal value As WorkDone) 
    handlers.Remove(value)  
  End RemoveHandler 

  RaiseEvent (ByVal completedWork As Integer) 
    If completedWork > 50 Then  
      For Each handler As WorkDone In handlers  
        handler.Invoke(completedWork)  
      Next  
    End If  
  End RaiseEvent  
End Event

This should help you customize your event handler, so that you can "see" the delegates being added to the event from within your class.

Thorsten Dittmar
Thanks, that's just what I was after.
Jules
A: 

For anyone interested, I chanced upon a way to perform this without the need for a custom event.

By suffixing the event name with Event, you have access to some properties and methods. So in Thorsten's example, I would refer to the event as WorkCompleted*Event*.

Specific to this question, there is the method GetInvocationList that returns a list of delegates attached to the event.

In addition to this, checking if WorkCompleted*Event* IsNot Nothing, will tell you whether there are handlers for the event without having to retrieve the invocation list.

eta: My italics are showing up as '*' when I submit the post. No idea why.

Jules