将一个控件的EventHandler分配给另一个控件 [英] Assigning EventHandler of one control to another

查看:59
本文介绍了将一个控件的EventHandler分配给另一个控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我如何在

表单上迭代控件集合,同时将他们的事件处理程序分配给同一表单上另一个相同的

控件集合。


到目前为止,感谢另一位程序员,我已经为这些房产做了很好的工作



类型ctrlType = subject.GetType();

ConstructorInfo cInfo = ctrlType.GetConstructor(Type.EmptyTypes);

控制retControl =(控制)cInfo。 Invoke(null);

foreach(PropertyInfo pInfo in

ctrlType.GetProperties(BindingFlags.Public | Binding Flags.Instance))

{

if(pInfo.CanWrite&&(pInfo.PropertyType.IsValueType ||

pInfo.PropertyType.Name ==" String"))

{

尝试

{

pInfo.SetValue(retControl,pInfo.GetValue(subject,n ull),null); < br $>
}

catch(例外情况)

{

MessString + =(&quo t;无法指定的值 + pInfo.Name +"

\ n对象:\ t" + subject.Name +" \ nOf类型:\t" + ctrlType.Name +

" \\\
Because:\t" + ex.Message +" \ n \ nn");

}

}

}

如您所见,目标控件在运行时调用,上面的代码在另一个循环中调用
。我还改进了一些正确分配

数据绑定的代码。所有缺少的是事件的分配

处理程序。


任何想法?

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?

推荐答案

我不确定你是不是想通过简单的

Cloning做任何事情,但调用列表不能从课外列出

因为事件创建了一个委托的私有实例来保存

调用列表。你当然可以使用反射来强制它。


-

问候,


Tim Haughton


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

" Christopher Weaver" <我们***** @ nospamverizon.net>在消息中写道

新闻:%2 *************** @ tk2msftngp13.phx.gbl ...
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...
任何人都可以告诉我如何在
a表单上迭代一系列控件,同时将他们的事件处理程序分配给同一表单上另一个相同的集合
控件。

到目前为止,感谢另一个程序员,我已经为这些属性提供了很好的


类型ctrlType = subject.GetType();
ConstructorInfo cInfo = ctrlType.GetConstructor(Type。 EmptyTypes);
控制retControl =(控制)cInfo.Invoke(null);
foreach(PropertyInfo pInfo in
ctrlType.GetProperties(BindingFlags.Public | Binding Flags.Instance))
{
if(pInfo.CanWrite&&(pInfo.PropertyType.IsValueType ||
pInfo.PropertyType.Name ==" String"))
{
尝试
{
pInfo.SetValue(retControl,pInfo.GetValue(subject,n ull),null);
}
catch(Exception ex)
{
MessString + =(无法指定的值) + pInfo.Name +"到
\ nObject:\ t" + subject.Name +" \ nOf类型:\t" + ctrlType.Name +
" \ n因为:\t" + ex.Message +" \ n\\\
");
}
}


如您所见,目标控件在以下位置调用:运行时,上面的代码在另一个循环中调用。我还改进了一些代码,可以正确地为数据绑定分配
。所有缺少的是分配
事件处理程序。

任何想法?
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?



Tim Haughton写道:
Tim Haughton wrote:
你当然可以使用反射来强制它。
You could of course brute force it using reflection.




你确定吗?我记得尝试过一次,虽然我没有给b $ b给它太多时间,但我觉得调用清单是

实际上是非常好的保护并且不能即使通过

反思,也无法访问。很有可能我错过了什么,但是你能否b / b
如果你知道的话请说明怎么做?

Oliver Sturm

-

omnibus ex nihibo ducendis sufficit unum

插入空格以防止谷歌电子邮件销毁:

MSN oliver @ sturmnet.org Jabber sturm @ amessage.de

ICQ 27142619 http://www.sturmnet.org / blog


" Oliver Sturm" <醇**** @ sturmnet.org>在消息中写道

news:xn *************** @ msnews.microsoft.com ...
"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?




嗨奥利弗,如果我们不是在交叉目的,这应该这样做......


使用System;

使用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 (对象发送者,EventArgs e)

{

Class2 c =发送者为Class2;

Console.WriteLine(" Handler call by" + c.name);

}

}


公共类Class2

{

公共字符串名称;

公共事件EventHandler MyEvent;


public Class2(字符串名称)

{

this.name = name;

}


public void Fire()

{

MyEvent(this,EventArgs.Empty);

}

}

}

-

问候,


Tim Haughton


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



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


这篇关于将一个控件的EventHandler分配给另一个控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆