A friendly user interface that must have drop-down menus, popup menus, toolbars and shortcut keys. The same function, the programmer may have to provide several ways of operation, such as text Copy, menu command ©, shortcut key CTRL + C, the Copy button on the tool bar, are the programmer to provide users with the operation, can greatly facilitate the user at different levels. However, adding one more way of doing this means adding code to respond to the event, as well as multiple operations that implement the unified function must be consistent, such as when the Clipboard is not empty, menu commands, shortcut keys, buttons, are selectable enabled state, and when there is no content on the Clipboard, The enabled state of these artifacts is false. Of course, if these details are not centrally controlled, the components that implement the same functionality must be consistent, with more code added and more complex controls.
Action is to manage the details of implementing a feature in a unified way, such as caption, Checked, Enabled, HelpContext, Hint, ImageIndex, SHORTCT, Visible, OnExecute, and other properties and events. While the programmer sets the Action property, the code onexecute that executes the action is also implemented. When you define an action and connect it to a menu item, or a button, the action-defined property and the OnExecute event immediately replace the original setting and the connection is established. With this connection, when the Action property changes, such as enabled, the component properties connected to it are automatically updated, and in turn, the onexecute of the action is invoked when the corresponding event of the component is executed.
Action and ActionList
Action is defined as the smallest functional unit that is used to centralize code management across different components.
ActionList is a component that manages multiple actions uniformly, and in the form design phase, using its pop-up menu, you can add and subtract actions and define the action's properties. Its own properties image connects a timagelist, indicating that the action it contains uses that timagelist to define the ImageIndex. ActionLink is an invisible artifact, but it is responsible for establishing a connection between the component and the action.
Action Properties and events
Properties and event-specific features
Caption title, can be used for menu items, Toolbutton and so on.
Category categories
Checked Setting selection status for menu items
Enabled sets the available state for all the artifacts connected to it
Index entry for HelpContext Help for all components connected to it
Hint IntelliSense, setting prompt content
ImageIndex setting a picture on a menu item and a button
Visible indicates whether the component with which it is connected can be displayed
ShortCut sets the shortcut key for the menu item.
OnExecute code to execute this action
Onhint code that is called when a perceived widget displays hint
OnUpdate code that is called when action updates the properties of a widget
Let's get to know tactionlist. Tactionlist is inherited from the tcustomaction, and it manages the defined action, which is the programmer's interface to action programming.
Create a new project file, add a tactionlist on the main form, a TMemo, a timagelist, a few buttons, and a few menu items. Double-clicking the tactionlist will show its editing interface. Then right-click to select New Standard action to display the predefined action category, which is used to indicate the category that the action belongs to, we can select a specific category as needed, and we introduce some of the standards action for file manipulation.
Most of the actions listed here are already defined, meaning that they can be used as long as they are added to the actionlist. Similarly, you can create a new action, add your own defined name to the category attribute, and then include a new action.
Connect action
After the action is defined, it is not connected with the menu item and button, is not used, and does not reach the centralized implementation and management of the program function. Using the Object Viewer, look at the menu item we just selected and whether there is a property action on the button. Within the action properties of these artifacts, after selecting an action, many of the properties of the component itself are changed with the properties defined by the action, such as the caption of the menu item and the action's property caption. The properties of the component name and tag are not changed with. Once connected, the property value of the action is copied to the corresponding property of the connected component, which is dynamically updated to automatically update the associated component when the action's property value changes.
Note: If you are using a Ttoolbutton or a menu item, you must set the images property associated with it yourself, although the ImageIndex property of the widget has been dynamically connected to the action, but the attribute images is not automatically connected.
Continue with the example in this section to complete the action connection by setting the Action property of the menu item and button.
Handling Action
The code that handles the action can be placed in the action's OnExecute event, or centrally placed in the onexecuteaction of the actionlist, and can be placed more centrally in the Onexecuteaction event of the application. Changes in the past each component must correspond to the event of the practice, programming more systematic.
Write a OnExecute method for some action. The OnExecute events such as Fileopencmd are as follows:
If Opendialog.execute Then
Memo1.loadfromfile (Opendialog.filename);
Update Action Property
Action events have OnExecute and Onupdate,onexecute events that respond when the control is triggered, such as when the button is pressed, the menu is pressed, and the OnUpdate event is called when the application is idle to update the action's properties. ActionList also has onupdate, which can be used as an optional property for setting menus or buttons. As an example, interested readers can look at it to get a better understanding of the action.
Procedure Tmainform.actionlist2update (action:tbasicaction; var handled:boolean);
Begin
editcutcmd.enabled: = editor.sellength > 0;
editcopycmd.enabled: = editcutcmd.enabled;
If editor.handleallocated then
begin
editundocmd.enabled: = Editor.perform (Em_canundo, 0, 0) <> 0;
editpastecmd.enabled: = Editor.perform (em_canpaste, 0, 0) <> 0;
END;
End;
executes this code when the application is idle, first checking for the selected text, setting the Enabled property of cut and copy to True if present, and the undo and paste properties when the current control is editor , Perfom is the method by which the component sends messages to itself.
Delphi has defined a number of standard actions that have been incorporated into the capabilities and VCL code of the action implementation. So, programmers can use it directly, and some actions don't even need to define OnExecute events. such as cut,copy,paste, such as shear plate operation, Cancel,delete,edit,first,insert,last,next,prior,post,refresh and other data navigation functions, Arrange,cascade, Close,minimizeall,tilehorizontal and other window arrangement functions