如何获取控件事件处理程序的方法名称 [英] how to get method name of control event handler

查看:104
本文介绍了如何获取控件事件处理程序的方法名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我想找到控制事件的方法名称
像这样

Hi all

I want to find method name of control event
like this

public partial class MyForm : Form
{
    public MyForm()
    {
        InitializeComponent();
        this.button1.Click += new System.EventHandler(this.SayHello);
    }

    protected void SayHello(object sender, EventArgs e)
    {
        MessageBox.Show("Hello!");
    }

    private void button2_Click(object sender, EventArgs e)
    {
        MessageBox.Show(FindHandlingControl("SayHello"));
    }

    private string FindHandlingControl(string EventHandler_MethodName)
    {
        EventHandler eh;
        foreach (Control c in this.Controls)
        {
            eh = (EventHandler)c.Click;
            if (eh.Method.Name == EventHandler_MethodName)
                return c.Name;  //Must Return button1
        }
        return "";
    }
}



编译时显示此错误:

事件"System.Windows.Forms.Control.Click"只能出现在+ =或-=的左侧


如何找到控制处理程序方法名称?

谢谢,



when compiling this error shown :

The event ''System.Windows.Forms.Control.Click'' can only appear on the left hand side of += or -=


how to find control handler method name?

thanks

推荐答案

好吧,我希望你已经准备好了,因为Microsoft确实不希望人们这样做:)

首先,我们捕获click事件并将其存储在对象机密中.除此之外,我们还将事件的属性信息存储在eventsProp中.

Alright, I hope you are ready because Microsoft REALLY doesn''t want people to do this :)

First we capture the click event and store it in an object secret. Besides that we store property information of events inside eventsProp.

System.Reflection.FieldInfo eventClick = typeof(Control).GetField("EventClick", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
object secret = eventClick.GetValue(null); 

System.Reflection.PropertyInfo eventsProp = typeof(Component).GetProperty("Events", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);



这里是对您的FindHandlingControl的重新访问.现在,我们创建控件所有事件的EventHandlerList.这将成为所有事件的数组.

然后,我们要捕获EventClick事件(存储在对象机密中).因此,如果您愿意的话,我们将创建一个事件数组插槽"EventClick"的委托点击.

然后,我们捕获了控件c的Click事件.如果没有针对控件的事件单击,则单击将为null,因此必须进行检查.

如果不为null,则将根据传递给FindHandlingControl函数的参数进行检查.如果正确,则将返回正在检查的当前控件的名称并将离开该函数.



And here is a revisit on your FindHandlingControl. We now create an EventHandlerList of all events of the control. This will become an array of all the events.

We then want to capture the EventClick event (stored in object secret). So we create a delegate click of the eventsarray slot "EventClick" if you will.

Then we have captured the Click event of the control c. If there is no event click for the control click will be null so this must be checked.

If not null it will be checked against the parameter passed into the FindHandlingControl function. If this is correct is will return the name of the current control that is being checked and will leave the function.

private string FindHandlingControl(string EventHandler_MethodName)
{
  foreach (Control c in this.Controls)
  {
    EventHandlerList events = (EventHandlerList)eventsProp.GetValue(c, null);
    Delegate click = events[secret];
    
    if (click != null)
    {
      if (click.Method.Name == EventHandler_MethodName)
      {
        return c.Name;  
      }
    }
  
  return "";
}



因此,所有代码总的来说:



So all code overall:

System.Reflection.FieldInfo eventClick = typeof(Control).GetField("EventClick", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
object secret = eventClick.GetValue(null); 
            
System.Reflection.PropertyInfo eventsProp = typeof(Component).GetProperty("Events", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); 
      
private string FindHandlingControl(string EventHandler_MethodName)
{
  foreach (Control c in this.Controls)
  {
    EventHandlerList events = (EventHandlerList)eventsProp.GetValue(c, null);
    Delegate click = events[secret];
    
    if (click != null)
    {
      if (click.Method.Name == EventHandler_MethodName)
      {
        return c.Name;  
      }
    }
  
  return "";
}



现在就结束了.我能问你为什么需要这个吗?



Now that that''s over with. Can I ask you why you need this?


谢谢,

因为我正在处理我的公司申请
每个按钮控件都具有用户访问权限
每个表格都可以捕获输入的键,并且所有按钮都有快捷键
如果用户使用键盘快捷键单击此键,我将无法应用安全性,
我需要知道哪个事件适合哪个按钮

请原谅我这个丑陋的描述

再次谢谢你
thank you,

because I am working on My Company Application
each button control has access permission for users
each form can capture key entered, and all button have shortcut key
if user click this key with keyboard shortcut i can not apply security,
i need to know which event is for which button

excuse me for this ugly description

thank you again


这篇关于如何获取控件事件处理程序的方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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