Action, the class and the property
Posted
#1
(In Topic #331)
Regular

Initially I was baffled by the purpose of the property Action of type String in the controls. I've since seen the Action class defined in the wiki as 'This class acts like a read-only static array.' and discovered that it can control properties of multiple controls, such as their Enabled property.
Posted
Guru

Posted
Regular

I've started using it to act on paired controls like a given toolbar-button + menu-item. In this case, the click event handlers of each call a sub containing this code, and the code disables those controls while something is happening, and also turns on/off the progress bar in 'marquee' mode (sorry, .Net term, 'pulse' in Gambas).
Updated: per tip below (Thanks cogier)
Posted
Guru

Posted
Regular

Steve
Posted
Guru

Action_Activate
This is a used as a procedure and if a button has an 'Action' value then this procedure is triggered. 'sValue' is the Action text:-
I'm not sure why 'As String' is needed but it does not work without it.
I didn't have much luck with this but Action.Raise(Control) may be what you need. This will trigger the procedure above passing it the Action text of the control.Action["action_name"].Raise
As in all good TV programs, here is an example I prepared earlier!
Posted
Regular

This is what I came up with too…
Code (gambas)
- 'Note: I am not sure I see the benefit of using this dispatch mechanism over direct click events (I like the other aspects of Action a lot though).
- 'Maybe if the actions were *directly executed* by the controls *for me* I'd be more sold on the idea.
- 'Having to wire up the dispatching manually seems brittle. But it does work!
- Select sActionName
- Case "FileNew"
- FileNew
- Case "FileOpen"
- FileOpen
- '...
- Case "HelpContents"
- HelpContents
Posted
Regular

My initial dislike of having to write a dispatcher in the Activate event is balanced somewhat by realizing that I would still have to write event handlers if the controls firing an Action ultimately caused it to call some code directly. (I was hoping that some method would be called directly by action name, but that might not be workable.)
I was playing with Lazarus Pascal this week for the first time in a long while and saw that objects of type TAction would fire their defined OnExecute event when the referencing control (menu, button) was clicked. But that jsut ended up being a whole bunch of separate event handlers too. Don't know if that's better than a single dispatcher or not.
In both languages, I like the idea of a single Action tied to one or more controls, and managing multiple properties in those controls (Caption, Tooltip, Picture, Enabled, Shortcut, etc.)
Posted
Guru

Note in my above example, I did not need to call the Raise method myself, as that is done behind the scenes by the control that is clicked.
I included it in my example as it shows how you can trigger the routine for a separate purpose.
I was hoping that some method would be called directly by action name, but that might not be workable.
If you use Action.Raise(Control) it will trigger the routine sending it the control's Action text, which is basically the same thing, or am I missing something?
Posted
Regular

cogier said
Note in my above example, I did not need to call the Raise method myself, as that is done behind the scenes by the control that is clicked.
I included it in my example as it shows how you can trigger the routine for a separate purpose.
I was hoping that some method would be called directly by action name, but that might not be workable.
If you use Action.Raise(Control) it will trigger the routine sending it the control's Action text, which is basically the same thing, or am I missing something?
Yes, I appreciate that, so we can see a working example of that in use.
As for the Raise in the context of a button/menu click, I get the impression that Raise fires the action but the button/menu fires that Raise method for us. My point is really about when the action is fired, it does not run a routine specific to the action (like maybe a SomeActionName_Activate), but a general one (Action_Activate). And as for me questioning whether there should be one or the other, its still better that having to code separate click events (button_Click, menu_Click) to call the same code, so I'm not really complaining, just working through the differences out loud between how Gambas and Lazarus do Action objects.
So far I am liking Gambas a lot more than Lazarus, even though the latter is not bad.
Posted
Administrator

If you want separate events getting triggered by different controls you should have a look at group.
You will find it in menu items as well as on controls.
<IMG src="https://paste.c-net.org/SufferedFlamingo">
<IMG src="https://paste.c-net.org/VomitWonderin">
Say you have a few controls in a group named "Test".
Right clicking one of the controls in this group and selecting Event -> Click will give you this code:
Now all controls in group Test will run this code when clicked on.
If you would right click one of the controls in the group Test again and select Event -> DblClick it would result in this code:
In this manner you can handle multiple events for a group of controls with the same event handler.
Quite handy.
Action I usually only use to enable/disable controls or make them visible or not.
My 2 cents on the matter.
gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories
… there is always a Catch if things go wrong!
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories
… there is always a Catch if things go wrong!
Posted
Regular

Thanks! I had not looked closely at Group because of the name, where in VB I might group controls like radio-buttons to act as different values associated with one field.
I'll have to have a closer look…
Steve
__________
gbWilly said
Hi,
If you want separate events getting triggered by different controls you should have a look at group.
You will find it in menu items as well as on controls.
…
Now all controls in group Test will run this code when clicked on.
If you would right click one of the controls in the group Test again and select Event -> DblClick it would result in this code:
…
In this manner you can handle multiple events for a group of controls with the same event handler.
Quite handy.
Action I usually only use to enable/disable controls or make them visible or not.
My 2 cents on the matter.
1 guest and 0 members have just viewed this.

