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. Managed C++/CLI
  4. Invoking Event Handlers Programmatically?

Invoking Event Handlers Programmatically?

Scheduled Pinned Locked Moved Managed C++/CLI
databasequestion
7 Posts 3 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.
  • T Offline
    T Offline
    TheBerk
    wrote on last edited by
    #1

    I'm not sure if this is possible, since I haven't been able to find much of anything on it, but I'm looking to call a control's event handlers programmatically. What I have is an application with multiple windows. Across the top of the app in a tool bar and one of those buttons, lets say copy, must preform that action on whichever window is currently active. They only way I can think to do this (as for various reasons the only way I can get these windows is as an instance of System::Windows::Forms::Control) is to trigger its Key Pressed event handler with 'Ctrl+C', but I can't seem to get access to it. Is this even possible? And if so how? Thanks!

    A L 2 Replies Last reply
    0
    • T TheBerk

      I'm not sure if this is possible, since I haven't been able to find much of anything on it, but I'm looking to call a control's event handlers programmatically. What I have is an application with multiple windows. Across the top of the app in a tool bar and one of those buttons, lets say copy, must preform that action on whichever window is currently active. They only way I can think to do this (as for various reasons the only way I can get these windows is as an instance of System::Windows::Forms::Control) is to trigger its Key Pressed event handler with 'Ctrl+C', but I can't seem to get access to it. Is this even possible? And if so how? Thanks!

      A Offline
      A Offline
      Andreoli Carlo
      wrote on last edited by
      #2

      So you press the button and want for example that in windows#1 it copy something, in windows#2 it copy something and so on.... if the toolbar is the 'creator' of the windows# you can use a list to have the pointer of all of the window you have created....then you can loop through the list and see which windows is active, and then call the specified function on that window....do i understand your problem?

      T 1 Reply Last reply
      0
      • A Andreoli Carlo

        So you press the button and want for example that in windows#1 it copy something, in windows#2 it copy something and so on.... if the toolbar is the 'creator' of the windows# you can use a list to have the pointer of all of the window you have created....then you can loop through the list and see which windows is active, and then call the specified function on that window....do i understand your problem?

        T Offline
        T Offline
        TheBerk
        wrote on last edited by
        #3

        Right, if the button is pushed the copy event is called in whatever window is currently active. I have the a active window part already, from the tool bar I have an 'active Window' pointer, so thats not a problem, but since that pointer is just a System::Windows::Forms::Control pointer, I don't have access to any of the controls specially written "Copy" functions, just the standard Forms::Control functions. All of the controls which have some sort of copy functionality already can do it through "Ctrl+C" so I wanted to know if I could programmatically trigger its KeyPress event handler to use their copy functionality. I'm just not sure how to trigger an event like keypress via code.

        A 1 Reply Last reply
        0
        • T TheBerk

          I'm not sure if this is possible, since I haven't been able to find much of anything on it, but I'm looking to call a control's event handlers programmatically. What I have is an application with multiple windows. Across the top of the app in a tool bar and one of those buttons, lets say copy, must preform that action on whichever window is currently active. They only way I can think to do this (as for various reasons the only way I can get these windows is as an instance of System::Windows::Forms::Control) is to trigger its Key Pressed event handler with 'Ctrl+C', but I can't seem to get access to it. Is this even possible? And if so how? Thanks!

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, IMO you should not attempt to call an event handler explicitly; one reason is you would have to provide fake parameters, sender wouldn't be a problem, the particular EventArgs may prove more difficult to fake. Unless of course you're absolutely sure the handler doesn't (and never will) use its parameters. There are two alternatives I would recommend: 1. some events can be triggered by calling an appropriate method, e.g. Button.PerformClick(). 2. when the functionality of an event handler is also useful outside the normel event handling, refactor it into a separate method, call that from the event handler, and from anywhere else you'd like to call it. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


          T 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, IMO you should not attempt to call an event handler explicitly; one reason is you would have to provide fake parameters, sender wouldn't be a problem, the particular EventArgs may prove more difficult to fake. Unless of course you're absolutely sure the handler doesn't (and never will) use its parameters. There are two alternatives I would recommend: 1. some events can be triggered by calling an appropriate method, e.g. Button.PerformClick(). 2. when the functionality of an event handler is also useful outside the normel event handling, refactor it into a separate method, call that from the event handler, and from anywhere else you'd like to call it. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


            T Offline
            T Offline
            TheBerk
            wrote on last edited by
            #5

            Thanks for the reply! My problem I have with you're second suggestion is that some of the windows contain third party controls, and are basically black boxes, so I cannot move that functionality into a seperate method. You're first suggestion seems promising, is there a way to do something like that, but with key stokes instead of Button.PerformClick()? Thanks!

            L 1 Reply Last reply
            0
            • T TheBerk

              Thanks for the reply! My problem I have with you're second suggestion is that some of the windows contain third party controls, and are basically black boxes, so I cannot move that functionality into a seperate method. You're first suggestion seems promising, is there a way to do something like that, but with key stokes instead of Button.PerformClick()? Thanks!

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              yes, you can send key strokes and mouse actions to any window you like, whether in your own app or another one. However may be tricky, and error prone; the user (assuming there is one) may well interfere with your automation (or vice versa), so I do not recommend it in general. If you must, there is the SendKeys class; its SendWait() method is useful. What I have been doing for mouse automation mostly is based on Win32 functions such as SetCursorPos() and SendInput(), both in user32.dll WARNING: localization changes the shortcut letters that can be used to steer menu's and buttons. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


              1 Reply Last reply
              0
              • T TheBerk

                Right, if the button is pushed the copy event is called in whatever window is currently active. I have the a active window part already, from the tool bar I have an 'active Window' pointer, so thats not a problem, but since that pointer is just a System::Windows::Forms::Control pointer, I don't have access to any of the controls specially written "Copy" functions, just the standard Forms::Control functions. All of the controls which have some sort of copy functionality already can do it through "Ctrl+C" so I wanted to know if I could programmatically trigger its KeyPress event handler to use their copy functionality. I'm just not sure how to trigger an event like keypress via code.

                A Offline
                A Offline
                Andreoli Carlo
                wrote on last edited by
                #7

                loaak at http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx[^] also...you black boxes give you the possibilities only to use the "control+c" key or they provide also some public function to do the operations? if they give also public function you can always try to static_cast or dynamic_cast the Control pointer...if this is your case this is far better than trigger the ctr+c command

                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