Re: Matching Event Handlers in an Interface
From: Charles Law (blank_at_nowhere.com)
Date: 06/19/04
- Next message: Herfried K. Wagner [MVP]: "Re: Best Place to Store Personal Settings"
- Previous message: One Handed Man \( OHM - Terry Burns \): "Re: Best Place to Store Personal Settings"
- In reply to: Charles Law: "Matching Event Handlers in an Interface"
- Next in thread: Jay B. Harlow [MVP - Outlook]: "Re: Matching Event Handlers in an Interface"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 19 Jun 2004 13:11:03 +0100
Could someone have a look at this please. It seems to me that it should be
possible to get a list of handlers for an event and then remove any handler
that is in a given object. I just can't figure out how to do it.
Anyone?
Charles
"Charles Law" <blank@nowhere.com> wrote in message
news:ehZO2N6UEHA.1652@TK2MSFTNGP09.phx.gbl...
> Mr "yEaH rIgHt" posted the following link about a week ago in answer to my
> question about removing event handlers.
>
> > http://www.vbinfozine.com/t_bindevt.shtml
>
> Following on from that post, the following issues still exist.
>
> The article shows how to find methods on a receiver that match the pattern
> OnXXXX given the sender. It loops through the sender events and tries to
get
> methods from the receiver that match the pattern. For each one it finds it
> either adds or removes a handler for the event.
>
> This is great where there is a straight forward mapping, but I don't have
a
> straight forward mapping. For example, although I could easily use this
> pattern in many cases, there are situations where the sender has two
similar
> events, only one of which a particular receiver is going to service.
>
> Let's say that my sender has an event ValueChanged. My receiver could
easily
> have a method OnValueChanged that could be linked up using the technique
> above. As it happens, my receiver has a method MyControl_ValueChanged, but
> this also works with the technique above.
>
> The problem arises where my sender has two values that can change. In this
> case it can raise two events: Value1Changed and Value2Changed. My receiver
> only services one of these, and which one is determined at runtime.
> Therefore, the receiver still has a method MyControl_ValueChanged, because
> it knows nothing of a sender that has two values.
>
> This breaks the pattern, as there is now easy way to transform the event
> Value2Changed into the method name MyControl_ValueChanged.
>
> So, ...
>
> I am trying to find a way of looping through the sender's events, as
before,
> but for each one I want to get the invocation list, and see if my receiver
> is in it. Only if it is do I want to remove the handler. I have got this
far
>
> <code>
> Public Shared Sub RemoveHandlers(ByVal sender As Object, ByVal
receiver
> As Object)
>
> Dim SenderType As Type
> Dim ReceiverType As Type
>
> Dim dlgts() As [Delegate]
>
> SenderType = sender.GetType()
> ReceiverType = receiver.GetType()
>
> For Each ei As EventInfo In SenderType.GetEvents()
>
> ' For each event that the sender can raise, get the invocation
> list
> dlgts = sender.Events.Item( *WHAT GOES
> HERE?* ).GetInvocationList()
>
> For Each dlgt As [Delegate] In dlgts
> If *TEST FOR DELEGATE BEING ON RECEIVER?* Then
> ei.RemoveEventHandler(sender, dlgt)
> End If
> Next dlgt
>
> Next ei
>
> End Sub
> </code>
>
> Can someone suggest what might replace the capitalised text?
Alternatively,
> can
> anyone think of another way of achieving the same end?
>
> Thanks.
>
> Charles
>
>
- Next message: Herfried K. Wagner [MVP]: "Re: Best Place to Store Personal Settings"
- Previous message: One Handed Man \( OHM - Terry Burns \): "Re: Best Place to Store Personal Settings"
- In reply to: Charles Law: "Matching Event Handlers in an Interface"
- Next in thread: Jay B. Harlow [MVP - Outlook]: "Re: Matching Event Handlers in an Interface"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|