Click here to Skip to main content
15,891,847 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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 "";
    }
}


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
Posted
Updated 11-Apr-10 21:10pm
v2

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);


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?
 
Share this answer
 
v2
Comments
Member 9931874 5-Apr-13 15:55pm    
But if the control has more than one method in same event? How could I get their method names?
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900