473,385 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Assigning EventHandler of one control to another

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
9 3059
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
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
"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sandor Heese | last post by:
Question, When using BeginInvoke (on a From to marshal to the UI thread) with the EventHandler delegate I see some strange behaviour. The problem is that I call the UpdateTextBox method with a...
0
by: dominosly | last post by:
Okay, this is a rather complicated problem, so here is the short of it: I am using a custom designed user control that simply writes out a plain old html submit button to the page. When the...
5
by: Torben | last post by:
For test purposes I attach an event to a control, say a TextBox TextChanged event. At another time the same event delegate is attached to some other control, maybe a listbox. Same event function...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
1
by: Alessandro Rossi | last post by:
Hi, I am having this problem: I have developed a composite component which has 2 components: a textbox and a button. I need to add an eventhandler to a button click. I have added the eventhandler,...
2
by: mawi | last post by:
Hi there, When removing page children controls created dynamically not in last-to-first order, the close button of the last control looses its event wiring, even though the handler is rewired...
2
by: Vi | last post by:
Hi, I'm building a screen where I want to allow users to be able to see data one page at a time. The users would view the data by clicking on the Page Nr. The problem is that the number of pages...
7
by: OfurGørn | last post by:
At runtime I generate 8 pictureboxes, when the user click on a picturebox I fire an onclick eventhandler. Because these pictureboxes are generate at runtime I do not have direct access to the their...
3
by: moondaddy | last post by:
C# 2.0 I have declared some event arguments like this: public class ModeStateChangedEventArgs : EventArgs { Gen.DataState myState; public ModeStateChangedEventArgs() { }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.