首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# 如何得到Click事件的委托

2012-03-28 
C# 怎么得到Click事件的委托如题[解决办法]通过消息[解决办法]不理解问题[解决办法]是给click加委托?得到

C# 怎么得到Click事件的委托
如题

[解决办法]
通过消息
[解决办法]
不理解问题
[解决办法]
是给click加委托?得到是啥意思
[解决办法]
我记得应该可以写一个监听去控制事件的委托。
太久没接触.net了 我目前也忘的差不多了。
同学习。
[解决办法]
Control.Click += Yourdelegate() ;
[解决办法]
不明白楼主提的问题什么意思。
[解决办法]
如果要调用的话,最简单就是btnView_Click(Null,Null);
[解决办法]

探讨
是给click加委托?得到是啥意思

[解决办法]
Button btn=new Button();
btn.Click += new EventHandler(click);

protected void click(object sender, EventArgs e)
{
}
[解决办法]
C# code
using System;using System.Collections.Generic;using System.Reflection;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication2{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            MessageBox.Show("button1");        }        private void button2_Click(object sender, EventArgs e)        {            Delegate[] _List = GetObjectEventList(button1, "EventClick", typeof(Control));            foreach (var item in _List)            {                item.DynamicInvoke(sender, e);            }        }        public static Delegate[] GetObjectEventList(object p_Object, string p_EventName, Type p_EventType)        {            PropertyInfo _PropertyInfo = p_Object.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);            if (_PropertyInfo != null)            {                object _EventList = _PropertyInfo.GetValue(p_Object, null);                if (_EventList != null && _EventList is EventHandlerList)                {                    EventHandlerList _List = (EventHandlerList)_EventList;                    FieldInfo _FieldInfo = p_EventType.GetField(p_EventName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.IgnoreCase);                    if (_FieldInfo == null) return null;                    Delegate _ObjectDelegate = _List[_FieldInfo.GetValue(p_Object)];                    if (_ObjectDelegate == null) return null;                    return _ObjectDelegate.GetInvocationList();                }            }            return null;        }    }}
[解决办法]
是想得到 Click 这个event吧,event 就是委托,不过包了一层,你得到了也干不了啥,出了加减委托
[解决办法]
C# code
using System;using System.Collections.Generic;using System.Reflection;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication2{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            MessageBox.Show("button1");        }        private void button2_Click(object sender, EventArgs e)        {            Delegate[] _List = GetObjectEventList(button1, "EventClick", typeof(Control));            foreach (var item in _List)            {                MessageBox.Show(item.Method.ToString());            }        }        public static Delegate[] GetObjectEventList(object p_Object, string p_EventName, Type p_EventType)        {            PropertyInfo _PropertyInfo = p_Object.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);            if (_PropertyInfo != null)            {                object _EventList = _PropertyInfo.GetValue(p_Object, null);                if (_EventList != null && _EventList is EventHandlerList)                {                    EventHandlerList _List = (EventHandlerList)_EventList;                    FieldInfo _FieldInfo = p_EventType.GetField(p_EventName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.IgnoreCase);                    if (_FieldInfo == null) return null;                    Delegate _ObjectDelegate = _List[_FieldInfo.GetValue(p_Object)];                    if (_ObjectDelegate == null) return null;                    return _ObjectDelegate.GetInvocationList();                }            }            return null;        }    }} 


[解决办法]
你只能注册(+=)或者解除注册(-=),得到二字从何说起?
[解决办法]

探讨
请问下(caozhy兄台)
item.DynamicInvoke(sender, e);
DynamicInvoke怎么找不到

[解决办法]
我知道了,应该是 C# 4 (VS2010)新增的。

你可以获得 MethodInfo 用传统的反射来调用。

如果不需要调用,15L 的代码即可。
[解决办法]
C# code
        private void button2_Click(object sender, EventArgs e)        {            Delegate[] _List = GetObjectEventList(button1, "EventClick", typeof(Control));            foreach (var item in _List)            {                MethodInfo mi = item.Method;                mi.Invoke(this, new object[] { sender, e });            }        }
[解决办法]
还是各种方法
[解决办法]
什么控件?
[解决办法]
很奇怪。。。我的代码在我这里运行正常啊。
[解决办法]
利用反射技术可以获取,偶对这块也不是很熟哈
[解决办法]
探讨

C# code
using System;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using Sys……

[解决办法]
[MSDN]:

Delegate.DynamicInvoke 方法

版本信息
.NET Framework
受以下版本支持:3.5、3.0 SP1、3.0、2.0 SP1、2.0、1.1、1.0
 
探讨
我知道了,应该是 C# 4 (VS2010)新增的。

你可以获得 MethodInfo 用传统的反射来调用。

如果不需要调用,15L 的代码即可。

[解决办法]
楼主的意思是:btn控件的属性栏里面的事件是吧?
对不起,我是才来的超级菜鸟,请大家关照啊!

热点排行