This thread is a follow-on to my discussion with cogier in the Components forum about the meaning and purpose of Action.
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.
Action, the class and the property
- cogier
- Site Admin
- Posts: 1128
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Action, the class and the property
Have a look at the attached.
Re: Action, the class and the property
Thanks! It's a handy way to affect groups of controls.
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).
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).
...
StartProgressBarWithPicture("Printing...", Null, Action["FilePrint"].Picture, True, 33)
Action["FilePrint"].Enabled = False
...
Finally
Action["FilePrint"].Enabled = True
StopProgressBar(sStatusMessage)
...
Updated: per tip below (Thanks cogier)
Last edited by sjsepan on Sunday 27th October 2019 1:51pm, edited 1 time in total.
- cogier
- Site Admin
- Posts: 1128
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Action, the class and the property
Tip - Use the 'gb' button for adding Gambas code to the forum it looks a lot better.
...
StartProgressBarWithPicture("Printing...", Null, Action["FilePrint"].Picture, True, 33)
Action["FilePrint"].Enabled = False
...
Finally
Action["FilePrint"].Enabled = True
StopProgressBar(sStatusMessage)
...
Re: Action, the class and the property
Does anyone have any working examples of how Action["action_name"].Raise and Action_Activate are used?
Steve
Steve
- cogier
- Site Admin
- Posts: 1128
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Action, the class and the property
What are Sunday afternoons if not for but looking at Gambas code. I have learnt a bit here.
As in all good TV programs, here is an example I prepared earlier!
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:-Action_Activate
Public Sub Action_Activate(sValue As String) As String
End
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!
Re: Action, the class and the property
I'll have to look that over, Thanks!
This is what I came up with too...
This is what I came up with too...
Public Function Action_Activate(sActionName As String) As Boolean
'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
Case Else
Print Subst("Action_Activate: &1", sActionName)
End Select
Return False
End
Re: Action, the class and the property
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.
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.)
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.)
- cogier
- Site Admin
- Posts: 1128
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Action, the class and the property
I included it in my example as it shows how you can trigger the routine for a separate purpose.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.
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?I was hoping that some method would be called directly by action name, but that might not be workable.
Re: Action, the class and the property
cogier wrote: ↑Sunday 17th November 2019 4:28pmI included it in my example as it shows how you can trigger the routine for a separate purpose.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.
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?I was hoping that some method would be called directly by action name, but that might not be workable.
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.