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?