Posts

Showing posts from 2011

How to copy event handlers from one control to another

Today at work, one of my partners in crime were working on a crazy customer requirement and ask me if I think that would be possible to copy event handlers from one control to another at run time and keeps things working correctly. I said yes, I think it’s possible and must be easy too, let’s take a look…
After a couple of minutes, we figure out that the Control class doesn´t expose any public method or property to get access to the collection of delegates attached to the control’s events.
I did a fair amount of web research and found a lot of partial solutions but none that fit our needs. So I decide to roll my own solution, post it, and may help some who is facing the very same problem.
Normally when we are working with events, we hook them up with handlers, writing a piece of code like this:
textBox1.TextChanged += (s, e) => MessageBox.Show("Hi there from handler 1");It’s also common to attach more than one handler to the same event:textBox1.TextChanged += (s, e) => Me…