I’m busy adding the menu handlers for an application and would love to be able to call the mousedown event for some buttons or other events for some different controls that correspond with the menu for the same. It would simplify duplicating code of having to create new functions or subs for each button so I could call them from either place, or is there some better way of c]connecting menu handlers to existing control events that I am unaware of?
Officially: no, you can’t. Inofficially: via introspection. I’ve done it with introspection don’t: it’s not worth it. It’s complicated when an application gets big, it’s not documented by Xojo so it’s not future-safe, and it doesn’t help understanding the structure of your application too much magic stuff going on.
Creating a method which is called from a button and a menu handler (and maybe also a toolbar item and a contextual menu) is the right thing to do. There is no duplication of code. You create a method with a command, then you call that command from several places. The code for a command doesn’t belong in the button’s Action event, nor does it belong in the menu handler. In my applications all code in the events of a window instance and its controls are one-liners calling command methods on the window. It’s like a self-documentation of the purpose of the window. Looking at the method names on my window instance a year later I know immediately what the window is for.
For Pushbuttons you can call Push to run their Action event (even shows the button being pressed). Otherwise what Eli said.
I’ve never seen this done, could you provide a brief example (the docs are worthless for this one). Thanks in advance!
Documentation always seems to be an easy target but I don’t think it’s fair to blame the them in this case.
How would you suggest they be improved?
I’m going to jump in because this comes up every now and then. If you find yourself wanting to call a control events it means you’re doing something wrong. Rather than thinking about calling an EventHandler, think about calling a common method called from those event handlers.
I have Pushbutton on a window that performs some action so I hook up the action event in that button. Then I add a toolbar that can invoke the same button. Then I add a Menu Item into the menu bar to do the same action. I can either duplicate the code in all three places (really bad idea), or call the Push method of the button. This works okay for buttons. But what if you have another type of control?
Instead, create a method called from each event handler. I often have a Handle_X_Action method that is called from the various event handlers.
If I had any say I would eliminate the Push method from the framework because it allows for some bad coding scenarios. It’s a crutch when it’s just as simple to create a common method.
Feel free to disagree but if any of my developers used Push I would seriously think about their future with me as an employee.
They’d get a push
You don’t like it when they push it too far, uh ?
…out the door.
I was thinking “in front of oncoming traffic”, “under bus”, or maybe “into open fire pit” but the rest work too
Thanks for all the great input, even the “pushy” kind. I’m using bevel buttons and push doesn’t with them and has a questionable future as Michel suggested. While it is more work it is cleaner engineering wise to use a single method to do the work and call it from all the places I need it done. I should have designed it like that from that start … poor engineering on my part. Live and learn.
No worries. When you’re starting out it’s easy to make those types of decisions. They’re not ‘mistakes’ since they’re part of the framework (for right or wrong). The trick is realizing that it’s not the best way of doing it. I just happen to have a particular affinity for this issue having fixed it way too many times.