Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Don't know if this is language specific, so please move if required.

 

Scenario:

Loads of UserControls with loads of "IExtendedControls" on them, which are often added at runtime.

 

IExtendedControls can raise an Event "ShowHistory (Sender as Object, e as ShowHistoryEventArgs)".

 

Quite naturally I do not want to manually add handlers to each and every IExtendedControl. I am doing this with a helper class, which will receive a root-control, a delegate and the will recurse through the root-controls control collection, check if the collection members implement IExtended and add the handler.

 

Now this:

Most Probably "ShowHistory" will not remain the only event that I want to fetch. So it would be quite convenient if I could also dynamically pass the event to the helper class.

 

I don't see this is possible within the framework.

Maybe I could pass the Event "ByName" and then use System.Reflection to find out, if an object can raise the desired event. Still I don't have a way to describe this event in an

 

AddHandler Object as Event, Object as Delegate

 

statement. (VB.NET says, there is no such type as "Event")

.nerd
Posted

Thanks for answering - because your answer made me doubt I was using delegates correctly and indeed I didn't.

 

However .... :)

 

... passing methods and events as delegates won't help me with the isse because the events do have different eventarg classes, thus different signatures, so the Eventhandlers have different signatures as well.

 

 

But, on the other hand, I can't even figure out how to do it with one specific delegate. I read a thread here on "delegates, eventhandlers, oh my" and I tried to copy it, but It still wouldn't work. :(

 

Public Class AutoHandlerAttacher
   Public Sub New(ByVal Parent As Control, ByRef Method As ShowHistoryHandler)
       mParent = Parent
       mMethod = Method
   End Sub

   Private mParent As Control
   Private mMethod As ShowHistoryHandler

   Public Delegate Sub ShowHistoryHandler(ByVal Sender As Object, ByRef e As ShowHistoryEventArgs)
   Private Sub Dummy(ByVal Sender As Object, ByRef e As ShowHistoryEventArgs)

   End Sub
)

   Private Sub AttachHandler()

       Dim aCtrl As Control
       Dim aIECtrl As IExtendedControl

       Dim typeIEC As Type = GetType(DSO.isys.ControlLibrary.IExtendedControl)

       '-------------------------------------------
       For Each aCtrl In mParent.Controls
           If typeIEC.IsInstanceOfType(aCtrl) Then
               aIECtrl = CType(aCtrl, IExtendedControl)
               AddHandler aIECtrl.ShowHistory, mMethod
               AddHandler aIECtrl.ShowHistory, AddressOf Me.Dummy
           End If
       Next

   End Sub

End Class

 

Works with the Dummy. Won't work with the delegate. (Can not cast between my delegate's type and the eventhandlers required type. Bummer.

 

Any hints?

.nerd
  • *Gurus*
Posted

I believe that first AddHandler line should be something like this:

 

AddHandler aIECtrl.ShowHistory, New ShowHistoryHandler(AddressOf mMethod)

 

It's probably because it's Friday evening but I'm not sure I fully understand what you're trying to do. If that doesn't help, could you outline it again with the code you have so far?

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

This is pretty much the code I have so far :) unless you want me to post the entire project (which I can't do, of course).

 

Your attempt didn't work either. Well nevermind, I have decided to tackle the problem in another manner. But thanks a lot anyway.

.nerd

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...