解决方案:C# 当按钮不可见时(Visible=False),调用Button.PerformClick事件无效!

解决方案
获取原按钮实例的Click事件,使用Invoke方式执行Click事件委托。
C# Code:
SimpleButton originalButton = (SimpleButton)GetButton((sender as SimpleButton).Tag.ToString());
//当originalButton不可见时,调用PerformClick事件无效!
//if (originalButton != null) originalButton.PerformClick();
//解决方案:获取原按钮实例的Click事件,使用Invoke方式执行Click事件委托
System.Reflection.PropertyInfo propertyInfo = (typeof(Control)).GetProperty("Events", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
System.Reflection.FieldInfo fieldInfo = (typeof(Control)).GetField("EventClick", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(originalButton, null);
Delegate method = eventHandlerList[fieldInfo.GetValue(null)];
if (method != null)
{
//委托的调用列表
var list = method.GetInvocationList();
if (list.Count() == 1)
{
originalButton.Invoke(method, sender, e);
}
else
{
//若有多个Click事件,依次调用
foreach (Delegate d in list) originalButton.Invoke(d, sender, e);
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
//当originalButton不可见时,调用PerformClick事件无效!
//if (originalButton != null) originalButton.PerformClick();
//解决方案:获取原按钮实例的Click事件,使用Invoke方式执行Click事件委托
System.Reflection.PropertyInfo propertyInfo = (typeof(Control)).GetProperty("Events", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
System.Reflection.FieldInfo fieldInfo = (typeof(Control)).GetField("EventClick", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(originalButton, null);
Delegate method = eventHandlerList[fieldInfo.GetValue(null)];
if (method != null)
{
//委托的调用列表
var list = method.GetInvocationList();
if (list.Count() == 1)
{
originalButton.Invoke(method, sender, e);
}
else
{
//若有多个Click事件,依次调用
foreach (Delegate d in list) originalButton.Invoke(d, sender, e);
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761

扫一扫加微信


版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网