Invoking Event Handlers Programmatically?
-
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!
-
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!
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?
-
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?
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.
-
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!
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]
-
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]
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!
-
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!
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]
-
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.
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