By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,728 Members | 2,462 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,728 IT Pros & Developers. It's quick & easy.

Assigning EventHandler of one control to another

P: n/a
Can anyone tell me how I could iterate through a collection of controls on a
form while assigning their event handlers to another identical collection of
controls on the same form.

So far, thanks to another programmer, I've got this working out quite nicely
for the properties:

Type ctrlType = subject.GetType();
ConstructorInfo cInfo = ctrlType.GetConstructor(Type.EmptyTypes);
Control retControl = (Control)cInfo.Invoke(null);
foreach(PropertyInfo pInfo in
ctrlType.GetProperties(BindingFlags.Public|Binding Flags.Instance))
{
if (pInfo.CanWrite && (pInfo.PropertyType.IsValueType ||
pInfo.PropertyType.Name == "String"))
{
try
{
pInfo.SetValue(retControl,pInfo.GetValue(subject,n ull),null);
}
catch(Exception ex)
{
MessString += ("Could not assign the value of " + pInfo.Name + " to
\nObject:\t" + subject.Name + "\nOf Type:\t " + ctrlType.Name +
"\nBecause:\t" + ex.Message + "\n\n");
}
}
}

As you can see, the target controls are invoked at runtime, the above code
being called in another loop. I've also refined some code that assigns the
databindings correctly. All that is missing is that assignment of the event
handlers.

Any ideas?
Nov 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
I'm not sure you're trying to do anything that couldn't be done via simple
Cloning, but the invocation list cannot be listed from outside the class
since an event creates a private instance of a delegate to hold the
invocation list. You could of course brute force it using reflection.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton

"Christopher Weaver" <we*****@nospamverizon.net> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Can anyone tell me how I could iterate through a collection of controls on a form while assigning their event handlers to another identical collection of controls on the same form.

So far, thanks to another programmer, I've got this working out quite nicely for the properties:

Type ctrlType = subject.GetType();
ConstructorInfo cInfo = ctrlType.GetConstructor(Type.EmptyTypes);
Control retControl = (Control)cInfo.Invoke(null);
foreach(PropertyInfo pInfo in
ctrlType.GetProperties(BindingFlags.Public|Binding Flags.Instance))
{
if (pInfo.CanWrite && (pInfo.PropertyType.IsValueType ||
pInfo.PropertyType.Name == "String"))
{
try
{
pInfo.SetValue(retControl,pInfo.GetValue(subject,n ull),null);
}
catch(Exception ex)
{
MessString += ("Could not assign the value of " + pInfo.Name + " to
\nObject:\t" + subject.Name + "\nOf Type:\t " + ctrlType.Name +
"\nBecause:\t" + ex.Message + "\n\n");
}
}
}

As you can see, the target controls are invoked at runtime, the above code
being called in another loop. I've also refined some code that assigns the databindings correctly. All that is missing is that assignment of the event handlers.

Any ideas?

Nov 17 '05 #2

P: n/a
Tim Haughton wrote:
You could of course brute force it using reflection.


Are you sure about that? I remember trying it once, and although I didn't
give it too much time, I had the impression that the invocation list was
actually extremely well protected and couldn't be accessed, even by
Reflection. It's well possible that I missed something, but could you
please show how to do that if you know it?
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #3

P: n/a
"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:xn***************@msnews.microsoft.com...
Are you sure about that? I remember trying it once, and although I didn't
give it too much time, I had the impression that the invocation list was
actually extremely well protected and couldn't be accessed, even by
Reflection. It's well possible that I missed something, but could you
please show how to do that if you know it?


Hi Oliver, if we're not talking at cross purposes, this should do it...

using System;
using System.Reflection;
namespace ConsoleApplication2
{
class Class1
{
[STAThread]
public static unsafe void Main(string[] args)
{
Class2 bob = new Class2( "Bob" );
bob.MyEvent +=new EventHandler(Handler);
bob.Fire();

Class2 bill = new Class2( "Bill" );
FieldInfo field = bob.GetType().GetField( "MyEvent",
BindingFlags.NonPublic | BindingFlags.Instance );
EventHandler eh = field.GetValue( bob ) as EventHandler;
bill.MyEvent += new EventHandler(eh);
bill.Fire();
}

private static void Handler( object sender, EventArgs e )
{
Class2 c = sender as Class2;
Console.WriteLine( "Handler called by " + c.name );
}
}

public class Class2
{
public string name;
public event EventHandler MyEvent;

public Class2( string name )
{
this.name = name;
}

public void Fire()
{
MyEvent( this, EventArgs.Empty );
}
}
}

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #4

P: n/a
Tim Haughton wrote:
FieldInfo field = bob.GetType().GetField( "MyEvent",
BindingFlags.NonPublic | BindingFlags.Instance );


Thanks for the sample! Now, if this line was working in my own test
program like it does in yours, I wouldn't have had a problem with this
before :-) Two things:

1. How did you get the idea that you have to use NonPublic, although the field in question is defined public in Class2? I tried it and it doesn't work if you use Public instead, but I really can't guess why.

2. For some reason, in my sample this doesn't work - this was one of the things I tried myself. The thing is, I was trying to access the Click event handlers for a normal System.Windows.Forms.Button. The click event on that one (or rather, on the Control) is declared as a property, not a field. Now, given your sample, I tried again using all permutations of GetProperty and Public or NonPublic flags, and just for the fun of it I fell back on GetField and GetEvent :-) No go. I'm still assuming I'm missing some combination here... any ideas?
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #5

P: n/a
Oliver Sturm wrote:
1. How did you get the idea that you have to use NonPublic, although the field in question is defined public in Class2? I tried it and it doesn't work if you use Public instead, but I really can't guess why.
It isn't public, it's private. Fire up ILDasm or better, .Net Reflector
and have a look at my sample compiled into an assembly. There is a
public event, yes. But there's also a *private* EventHandler delegate
declared as an instance with the same name(MyEvent). Declaring the
event is essentially syntactic sugar. What's really happening is you're
creating a private EventHandler delegate, and creating public add and
remove methods to add and remove handlers. Essentially we're wrapping
the private EventHandler delegate up so clients can only use the += and
-=, i.e. the add and remove. Have a look under the hood and all becomes
clear. The add method simply combines the passed in delegate with the
existing delegate.
2. For some reason, in my sample this doesn't work - this was one of the things I tried myself. The thing is, I was trying to access the Click event handlers for a normal System.Windows.Forms.Button. The click event on that one (or rather, on the Control) is declared as a property, not a field. Now, given your sample, I tried again using all permutations of GetProperty and Public or NonPublic flags, and just for the fun of it I fell back on GetField and GetEvent :-) No go. I'm still assuming I'm missing some combination here... any ideas?


I've not looked at the Button class in depth, and my wife is glaring at
me because I'm supposed to be cooking tea ;) (Are you sure it's a
property?) But... I think it falls down to the same principle. If you
declare a public event, you're also committing to having a private
delegate, i.e. you're also committing to an implementation.

Like I said before, open up my sample in reflector, have a look at it.
Decompile it and see what's really happening.

--
Regards,
Tim Haughton
Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton

Nov 17 '05 #6

P: n/a
Hi Oliver, FYI I've written this solution up...

http://www.blogitek.com/timhaughton/...g_the_eve.html
--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton

"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:xn****************@msnews.microsoft.com...
Tim Haughton wrote:
FieldInfo field = bob.GetType().GetField( "MyEvent",
BindingFlags.NonPublic | BindingFlags.Instance );
Thanks for the sample! Now, if this line was working in my own test
program like it does in yours, I wouldn't have had a problem with this
before :-) Two things:

1. How did you get the idea that you have to use NonPublic, although the

field in question is defined public in Class2? I tried it and it doesn't
work if you use Public instead, but I really can't guess why.
2. For some reason, in my sample this doesn't work - this was one of the things I tried myself. The thing is, I was trying to access the Click event
handlers for a normal System.Windows.Forms.Button. The click event on that
one (or rather, on the Control) is declared as a property, not a field. Now,
given your sample, I tried again using all permutations of GetProperty and
Public or NonPublic flags, and just for the fun of it I fell back on
GetField and GetEvent :-) No go. I'm still assuming I'm missing some
combination here... any ideas?

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog

Nov 17 '05 #7

P: n/a
Hi Oliver, FYI I've written this solution up...

http://www.blogitek.com/timhaughton/...g_the_eve.html
--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton

"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:xn****************@msnews.microsoft.com...
Tim Haughton wrote:
FieldInfo field = bob.GetType().GetField( "MyEvent",
BindingFlags.NonPublic | BindingFlags.Instance );
Thanks for the sample! Now, if this line was working in my own test
program like it does in yours, I wouldn't have had a problem with this
before :-) Two things:

1. How did you get the idea that you have to use NonPublic, although the

field in question is defined public in Class2? I tried it and it doesn't
work if you use Public instead, but I really can't guess why.
2. For some reason, in my sample this doesn't work - this was one of the things I tried myself. The thing is, I was trying to access the Click event
handlers for a normal System.Windows.Forms.Button. The click event on that
one (or rather, on the Control) is declared as a property, not a field. Now,
given your sample, I tried again using all permutations of GetProperty and
Public or NonPublic flags, and just for the fun of it I fell back on
GetField and GetEvent :-) No go. I'm still assuming I'm missing some
combination here... any ideas?

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog

Nov 17 '05 #8

P: n/a
Hi Tim,

so, I know now why I didn't find the problem myself at the time. I didn't
take the time to actually analyze the issue in detail, with a sample like
yours - which shows significant differences to the Button sample I was
working with.

Obviously you are right about the visibility of the relevant member. The
problem is that the Button class, for instance, doesn't even have such a
member because it uses the Component.Events list for its events instead of
its own member.

My train of thought was really different at the time, and that's still
valid right now: I was trying to find a generic way to copy the invocation
list from a control's event, of which I would know nothing more than the
name. For example, I have a control with an event called "Click" - I want
to copy the invocation list from that one.

This still seems to be impossible, because the way to do it in each single
instance depends on a lot of implementation details. For example: in your
sample, it's enough to access a private member; in the Button case, I'd
have to access a private static member, which is used as a key into the
Events list, get the Events list itself and then I'd be able to read the
invocation list from it - for any other control, things might work
differently again. Nice to know that it's generally possible, but that
wasn't the generic solution I was originally looking for :-)

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #9

P: n/a
Hi Tim,

so, I know now why I didn't find the problem myself at the time. I didn't
take the time to actually analyze the issue in detail, with a sample like
yours - which shows significant differences to the Button sample I was
working with.

Obviously you are right about the visibility of the relevant member. The
problem is that the Button class, for instance, doesn't even have such a
member because it uses the Component.Events list for its events instead of
its own member.

My train of thought was really different at the time, and that's still
valid right now: I was trying to find a generic way to copy the invocation
list from a control's event, of which I would know nothing more than the
name. For example, I have a control with an event called "Click" - I want
to copy the invocation list from that one.

This still seems to be impossible, because the way to do it in each single
instance depends on a lot of implementation details. For example: in your
sample, it's enough to access a private member; in the Button case, I'd
have to access a private static member, which is used as a key into the
Events list, get the Events list itself and then I'd be able to read the
invocation list from it - for any other control, things might work
differently again. Nice to know that it's generally possible, but that
wasn't the generic solution I was originally looking for :-)

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.