解决方案:C# 当按钮不可见时(Visible=False),调用Button.PerformClick事件无效!
作者:C/S框架网|www.cscode.ne  发布日期:2021-03-21 13:00:24
  解决方案: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




C/S框架网|原创精神.创造价值.打造精品

扫一扫加微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务

上一篇 下一篇