Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a component in one project: i.e using UITypeEditor to set some custom properties for a form.

In my another project Windows forms applicaiton.
I have added the component to the toolbox and using in the forms.
Right now I am getting the Parent of the component and All the controls of the parent form.

But I want to list the control events used in the parent form in design time?

What I have tried:

Here is my code I have tried..

C#
// Function to get the parent form
 public  Form setUpParentForm()
    {
             Form _parentForm = null;

       // if (_parentForm != null) return; // do nothing if it is set
       System.ComponentModel.Design.IDesignerHost host;
       if (Site != null)
       {
           host = Site.GetService(typeof(System.ComponentModel.Design.IDesignerHost)) as System.ComponentModel.Design.IDesignerHost;
           MessageBox.Show(host.ToString());
           object obj = host.GetService(typeof(Assembly)) as Assembly;
           
           if (obj != null)
           {

           }
           if (host != null)
           {

               if (host.RootComponent is Form)
               {
                   _parentForm = (Form)host.RootComponent;

                   MessageBox.Show("Host RootComponent ClassName: " + host.RootComponentClassName);
             
               }
           }
        
       }

        

        return _parentForm;
    }

// Function to get the control events list
 public static List<delegate> GetEventHandlerList(Form HostForm)
       {
           List<delegate> delegates = new List<delegate>();
           foreach (Control c in HostForm.Controls)
           {
               MessageBox.Show(c.Name.ToString());
               EventHandlerList events = (EventHandlerList)typeof(Component)
                .GetField("events", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField | BindingFlags.Public)
                .GetValue(c);

             
               object current = events.GetType()
                      .GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField | BindingFlags.Public)[0]
                      .GetValue(events);

              // MessageBox.Show("Current : " + current.ToString());
               while (current != null)
               {

                   delegates.Add((Delegate)GetField(current, "handler"));
                   current = GetField(current, "next");
               }

           }

           foreach (Delegate di in delegates)
           {
               MessageBox.Show(di.Method.ToString());
           }
           return delegates;
       }


       public static object GetField(object listItem, string fieldName)
       {
           return listItem.GetType()
              .GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField)
              .GetValue(listItem);
       }
Posted
Updated 22-Feb-18 19:54pm
v2
Comments
Ralf Meier 23-Feb-18 7:01am    
I think that you basicly refer to this Article from CP :
https://www.codeproject.com/Articles/34990/Get-Delegate-from-Event-s-Subscription

I suggest that you work with it complete. In your code you don't have a connection to then Control (only the loop which iterates through the Controls-Collection of the Form).

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