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. Setting events - beginner question

Setting events - beginner question

Scheduled Pinned Locked Moved C#
csharpdatabasecomdesignxml
5 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.
  • N Offline
    N Offline
    Neil Lamka
    wrote on last edited by
    #1

    I'm trying to educate myself on C# and .Net. I decided to try and use the techniques outlined by Marc Clifton ("Using XML to generate Menus") but to use it with a different UI tool kit along with extending the menus.xsd to support imagelists. What I'm looking for is a method of taking an input name (of an imagelist or a method) passed in the xml definition or the menu and use that name to pass the actual object or method to the appropriate constructors etc. I've found that I can find the appropriate MethodInfo information using reflection and can find the FieldInfo information for an imagelist but I can't seem to figure out how to use that information to accomplish my objective. I'm using the Crownwood Magic Library menus that have a MenuCommand constructor that will take an imagelist and index value for the menu image and will take an EventHandler for an OnClick event. Is there a way to use the information retrieved form the reflection methods to set the event handler and to set the imagelist from the names passed in the xml. Thanks for the help Neil Lamka neil@meetingworks.com

    B 1 Reply Last reply
    0
    • N Neil Lamka

      I'm trying to educate myself on C# and .Net. I decided to try and use the techniques outlined by Marc Clifton ("Using XML to generate Menus") but to use it with a different UI tool kit along with extending the menus.xsd to support imagelists. What I'm looking for is a method of taking an input name (of an imagelist or a method) passed in the xml definition or the menu and use that name to pass the actual object or method to the appropriate constructors etc. I've found that I can find the appropriate MethodInfo information using reflection and can find the FieldInfo information for an imagelist but I can't seem to figure out how to use that information to accomplish my objective. I'm using the Crownwood Magic Library menus that have a MenuCommand constructor that will take an imagelist and index value for the menu image and will take an EventHandler for an OnClick event. Is there a way to use the information retrieved form the reflection methods to set the event handler and to set the imagelist from the names passed in the xml. Thanks for the help Neil Lamka neil@meetingworks.com

      B Offline
      B Offline
      Bo Hunter
      wrote on last edited by
      #2

      I beleave you are on the wright track you just need to use the typeof( YourType ).GetMethod( "MethodName", BindingFlags.Public ).Invoke( objToCallInvokeOn, new object[](arg1, arg2, arg3} ); The BindingFlags is just that there flags so you can use | operator. The args can be null new object[]{null} or null will work I beleave. There are a few different ways and method overloads that is avaliable.

      N 1 Reply Last reply
      0
      • B Bo Hunter

        I beleave you are on the wright track you just need to use the typeof( YourType ).GetMethod( "MethodName", BindingFlags.Public ).Invoke( objToCallInvokeOn, new object[](arg1, arg2, arg3} ); The BindingFlags is just that there flags so you can use | operator. The args can be null new object[]{null} or null will work I beleave. There are a few different ways and method overloads that is avaliable.

        N Offline
        N Offline
        Neil Lamka
        wrote on last edited by
        #3

        Thanks for the pointer. If I use this method then I'm assuming that in my DLL that handles reading the XML definitions for menus and all that I'd have 1) Set an "OnClick" event handler internal to my DLL and then 2) When processing the OnClick event figure out the real target menthod that should process the event and use the Invoke as you described. Is there any way to set the event handler for the menu item so as to bypass having to handle the event and route it? I'd like to get out of the way and just wire the menu directly to the desired handler. I was hoping that once I found the MethodInfo for the target method I would be able to set the menu event handler directly. Or is that what your note allows me to do and I just did not understand. Thanks Neil Neil Lamka neil@meetingworks.com

        B 1 Reply Last reply
        0
        • N Neil Lamka

          Thanks for the pointer. If I use this method then I'm assuming that in my DLL that handles reading the XML definitions for menus and all that I'd have 1) Set an "OnClick" event handler internal to my DLL and then 2) When processing the OnClick event figure out the real target menthod that should process the event and use the Invoke as you described. Is there any way to set the event handler for the menu item so as to bypass having to handle the event and route it? I'd like to get out of the way and just wire the menu directly to the desired handler. I was hoping that once I found the MethodInfo for the target method I would be able to set the menu event handler directly. Or is that what your note allows me to do and I just did not understand. Thanks Neil Neil Lamka neil@meetingworks.com

          B Offline
          B Offline
          Bo Hunter
          wrote on last edited by
          #4

          Sorry it took so long to get back. I hate to say it but I dont fully understand what it is you need to do. Just a little more info on what you need to do and maybe I can help some more. Bo Hunter

          N 1 Reply Last reply
          0
          • B Bo Hunter

            Sorry it took so long to get back. I hate to say it but I dont fully understand what it is you need to do. Just a little more info on what you need to do and maybe I can help some more. Bo Hunter

            N Offline
            N Offline
            Neil Lamka
            wrote on last edited by
            #5

            Ok, Let me try again. What I'd like to be able to do is (from a class in a managed DLL) to take a method name from a file, create a menu item and set the "OnClick" event to use that method. What I have been able to figure out is how to set an onclick event handler that resides in my DLL and then to use Invoke to route the event to the desired handler within the application. I was looking for a direct way to wire the event handler to the menu item without the intermediate handling. I have the MethodInfo for the desired (real) event handler but don't see any way to use that to set the OnClick event using that information. An example would be something like &File My_Handler .... in the DLL string caption = ... get the caption from the xml string onclick = ... get the handler from the xml MethodInfo mi = ... get the MethodInfo for "My_Handler" (from onclick) //* I'm using the MagicLibrary .. which is where the MenuCommand lives MenuCommand mc = new MenuCommand( caption ); mc.Click += ??? using the MethodInfo from above. What I do have is the above but with mc.Click += new EventHandler(menu_Click); //menu_Click is my internal handler) private void menu_Click(object sender, System.EventArgs e) { MethodInfo mi = .. get the Method info for the real handler .... mi.Invoke(_callingInstance, args); .... } So..the .Net of what I'm trying to do is to use what I can find out from Reflection to wire a menu item directly to the target event handler without having to use Invoke and this event redirection. Hope this helps Neil Lamka neil@meetingworks.com

            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