Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Find events bound on a control

Find events bound on a control

Scheduled Pinned Locked Moved C#
csharptutorialvisual-studiohelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    M Harris
    wrote on last edited by
    #1

    I am attempting to write a form serialiser in C#, it is a Visual Studio .Net 2005 addin. My current problem is getting what methods are set as bound to events on a control. For example, i bind button1_click(..) to button1, i can iterate through all the events in System.Windows.Forms.Button just fine, however i have not been able to determine how to find out what delegates that event has bound to it. Current code: private void GetEvents(string type, object oControl, int controlId) { Type t = oControl.GetType(); EventInfo[] events = t.GetEvents(); foreach (EventInfo evnt in events) { object value = envt; } } Setting of value was simply for me to drop a break point on to play with the EventInfo object.. Any assistance or links to a guide would be most welcome. - Mark Harris

    M 1 Reply Last reply
    0
    • M M Harris

      I am attempting to write a form serialiser in C#, it is a Visual Studio .Net 2005 addin. My current problem is getting what methods are set as bound to events on a control. For example, i bind button1_click(..) to button1, i can iterate through all the events in System.Windows.Forms.Button just fine, however i have not been able to determine how to find out what delegates that event has bound to it. Current code: private void GetEvents(string type, object oControl, int controlId) { Type t = oControl.GetType(); EventInfo[] events = t.GetEvents(); foreach (EventInfo evnt in events) { object value = envt; } } Setting of value was simply for me to drop a break point on to play with the EventInfo object.. Any assistance or links to a guide would be most welcome. - Mark Harris

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #2

      I see you haven't gotten any response. :( There's an EventHandlerType property in EventInfo that you can use to (according to MSDN): A read-only Type object representing the delegate event handler. You can create a delegate of that type like this:

      dlgt=Delegate.CreateDelegate(ei.EventHandlerType, eventSink, mi.Name);

      However, what you want is a list of the event handlers, right? You will want to use GetInvocationList on the event itself:

      event FooDlgt FooEvent;
      ...
      foreach(Delegate dlgt in FooEvent.GetInvocationList())
      ...

      Hopefully this is enough information to either create a delegate of the correct type or scan the invocation list. Marc Pensieve Functional Entanglement vs. Code Entanglement Static Classes Make For Rigid Architectures

      M 1 Reply Last reply
      0
      • M Marc Clifton

        I see you haven't gotten any response. :( There's an EventHandlerType property in EventInfo that you can use to (according to MSDN): A read-only Type object representing the delegate event handler. You can create a delegate of that type like this:

        dlgt=Delegate.CreateDelegate(ei.EventHandlerType, eventSink, mi.Name);

        However, what you want is a list of the event handlers, right? You will want to use GetInvocationList on the event itself:

        event FooDlgt FooEvent;
        ...
        foreach(Delegate dlgt in FooEvent.GetInvocationList())
        ...

        Hopefully this is enough information to either create a delegate of the correct type or scan the invocation list. Marc Pensieve Functional Entanglement vs. Code Entanglement Static Classes Make For Rigid Architectures

        M Offline
        M Offline
        M Harris
        wrote on last edited by
        #3

        Well, after giving myself a rather large headache over the last 2 days trying to work this out, i have come up with nothing. I just can't work this out, and I have to demo the software to the staff here tuesday next week! So here is what I'm having problems with as far as this goes: GetInvocationList looks like EXACTLY what I need, however i can't call it, as i dont have an event to call it on. Due to the fact that this is a serialiser, i'm going over every control on the form with code somewhat similar to this:

        GetControls(_form, 0);
        private void GetControls(object oControl, int parentId)
        {
        if (((Control)oControl).Controls.Count > 0)
        {
        foreach (Control childCtrl in ((Control)oControl).Controls)
        {
        // put basic details into the database, get a control id for it from the database

        		// get its properties
        		GetProperties("control", childCtrl, control\_id);
        		GetEvents("control", childCtrl, control\_id);
        		GetControls(childCtrl, control\_id);
        	}
        }
        

        }

        GetEvents is defined:

        private void GetEvents(string type, object oControl, int controlId)

        Since this control could be any type of control i get the events on it:

        Type t = oControl.GetType();
        EventInfo[] events = t.GetEvents();

        and then loop through them

        foreach (EventInfo evnt in events)
        {
        }

        Now I have an EventInfo for hte object. From this I can create a delegate if I'm so inclined (dlgt = Delegate.CreateDelegate(evnt.EventHandlerType, mi ); ) however, I get the problem of where does MemberInfo come from? I can't get it from the class that the control is in, because I don't know what this event needs to be bound to (moreover, this is what i'm actually trying to find out). I'd really love any ideas you have or if you could just point out where I'm going wrong (if the entire concept is not incorrect).

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups