c#订阅事件是用了Lambda,怎么取消订阅呢? 楼主添加订阅,然后立马就取消。应该直接panel.DoubleClick = new EventHandler((sender, e) =>{ }就行了,建议试一下。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主添加订阅,然后立马就取消。应该直接panel.DoubleClick = new EventHandler((sender, e) =>{ }就行了,建议试一下。c#里面的事件无法用=赋值啊 Action<object, EventArgs> pro = null;pro = (sender, e) =>{ //....... panel.DoubleClick -= pro;};panel.DoubleClick += pro panel1.DoubleClick += new EventHandler( (sender1, e1)=> { //...... Type t = panel1.GetType(); PropertyInfo pi = t.GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic); EventHandlerList ehl = (EventHandlerList)pi.GetValue(panel1, null); FieldInfo fieldInfo = (typeof(Control)).GetField("EventDoubleClick", BindingFlags.Static | BindingFlags.NonPublic); Delegate d = ehl[fieldInfo.GetValue(null)]; if (d != null) { foreach (Delegate temp in d.GetInvocationList()) { StackTrace st = new StackTrace(true); if (temp.Method == st.GetFrame(0).GetMethod()) ehl.RemoveHandler(fieldInfo.GetValue(null), temp); } } }); 参考:http://social.microsoft.com/Forums/zh-CN/9211ba35-001f-4319-a8e6-96e53995fbf9/c-?forum=visualcshartzhchshttp://www.csharpwin.com/csharpspace/10743r4702.shtml 楼主添加订阅,然后立马就取消。应该直接panel.DoubleClick = new EventHandler((sender, e) =>{ }就行了,建议试一下。c#里面的事件无法用=赋值啊对不起,我搞错了,把匿名委托改一下就可以,4楼的方法应该可以解决了。panel.DoubleClick -= pro;什么时候取消订阅,什么时候就可以用 在窗体上添加3个按钮,然后编写如下代码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Reflection;namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { button3.Click += (a, b) => MessageBox.Show("ok"); } private void button2_Click(object sender, EventArgs e) { var handlers = GetObjectEventList(button3, "EventClick", typeof(Control)); foreach (var item in handlers.Select(x => x as EventHandler)) button3.Click -= item; } private 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; } }} EventHandler handler = (object sender1, EventArgs e1) => { MessageBox.Show("regist"); }; private void button1_Click(object sender, EventArgs e) { this.button3.Click += handler; } private void button2_Click(object sender, EventArgs e) { this.button3.Click -= handler; } Winform 应用服务该怎么做? 如何实现gridview的更新,删除,而不直接是相对于数据库的操作 datetime小问题 ExecuteScalar 要求已打开且可用的连接。连接的当前状态为已关闭 SQL 多个表的查询 请教:用listbox多查询条件的搜索问题 C#中的 Substring 问题 可不可以把对象序列化进资源文件?因该怎样做? 在C#中表示本身窗体用什么? 如何改变trackerbar的size? uniqueidentifier的using引用集是什么? 新手求助~
应该直接
panel.DoubleClick = new EventHandler((sender, e) =>{
}就行了,建议试一下。c#里面的事件无法用=赋值啊
pro = (sender, e) =>
{
//.......
panel.DoubleClick -= pro;
};
panel.DoubleClick += pro
panel1.DoubleClick += new EventHandler(
(sender1, e1)=>
{
//...... Type t = panel1.GetType();
PropertyInfo pi = t.GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
EventHandlerList ehl = (EventHandlerList)pi.GetValue(panel1, null);
FieldInfo fieldInfo = (typeof(Control)).GetField("EventDoubleClick", BindingFlags.Static | BindingFlags.NonPublic);
Delegate d = ehl[fieldInfo.GetValue(null)];
if (d != null)
{
foreach (Delegate temp in d.GetInvocationList())
{
StackTrace st = new StackTrace(true);
if (temp.Method == st.GetFrame(0).GetMethod())
ehl.RemoveHandler(fieldInfo.GetValue(null), temp);
}
}
}
); 参考:
http://social.microsoft.com/Forums/zh-CN/9211ba35-001f-4319-a8e6-96e53995fbf9/c-?forum=visualcshartzhchs
http://www.csharpwin.com/csharpspace/10743r4702.shtml
应该直接
panel.DoubleClick = new EventHandler((sender, e) =>{
}就行了,建议试一下。c#里面的事件无法用=赋值啊
对不起,我搞错了,把匿名委托改一下就可以,4楼的方法应该可以解决了。
panel.DoubleClick -= pro;什么时候取消订阅,什么时候就可以用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
button3.Click += (a, b) => MessageBox.Show("ok");
} private void button2_Click(object sender, EventArgs e)
{
var handlers = GetObjectEventList(button3, "EventClick", typeof(Control));
foreach (var item in handlers.Select(x => x as EventHandler))
button3.Click -= item;
} private 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;
}
}
}
EventHandler handler = (object sender1, EventArgs e1) =>
{
MessageBox.Show("regist");
}; private void button1_Click(object sender, EventArgs e)
{
this.button3.Click += handler;
} private void button2_Click(object sender, EventArgs e)
{
this.button3.Click -= handler;
}