Sign in Join
Wiki copytemporary

How to use menu controls: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: How to use menu controls

OldNewLine
1+### TMainMenu
2+
3+[[TMainMenu]] is the Main Menu that appears at the top of most forms; form designers can customise by choosing various menu items.
4+
5+[[TMainMenu]] is a non-visual component: that is, if the icon is selected from the [[Component Palette]] and placed on the form, it will not appear at [[runtime|run time]]. Instead, a menu bar with a structure defined by the [[IDE Window: Menu Editor|Menu Editor]] will appear.
6+
7+### TPopupMenu
8+
9+[[TPopupMenu]] is a menu window that pops up with pertinent, usually context-sensitive, details and choices when the right mouse button is clicked on a control if the popupmenu is linked to the PopupMenu [[Property|property]] of that component, thus providing a context sensitive menu for that component.
10+
11+### Menu editor
12+
13+To see the [[IDE Window: Menu Editor|Menu Editor]], right-click on the Main Menu or Popup Menu icon on your Form. A pop-up box appears that invites you to enter items into the Menu bar.
14+
15+An edit box is displayed, containing a button labeled "New Item1". If you right-click on that box, a pop-up menu is displayed that allows you to add a new item before or after (along the same level) or create a sub-menu with the opportunity to add further items below (or above) the new item in a downward column.
16+
17+Any of the [[TMenuItem|TMenuItems]] that you add can be configured using the [[IDE Window: Object Inspector|Object Inspector]].
18+
19+At the least you should give each item a *Caption* which will appear on the Menu Bar. The caption should indicate the activity to be selected, such as "File Open" or "Close", "Run" or "Quit". You may also wish to give it a more meaningful *Name*.
20+
21+If you want a particular letter in the Caption to be associated with a shortcut key, that letter should be preceded by an [[&amp;|ampersand (&amp;)]]. The Menu item at run-time will appear with the shortcut letter underlined, and hitting that letter key will have the same effect as selecting the menu item. Alternatively you can choose a shortcut key sequence (such as <span class="kbd">Ctrl</span>+<span class="kbd">C</span> for Copy or <span class="kbd">Ctrl</span>+<span class="kbd">V</span> for Paste - the standard keyboard shortcuts) with the *ShortCut* property of the [[TMenuItem]].
22+
23+#### Setting Shortcuts Programmatically
24+
25+Two functions are provided that convert virtual key to shortcuts and visa versa, KeyToShortCut() and ShortCutToKey(). eg
26+
27+``` pascal
28+MenuBold.ShortCut:= KeyToShortCut(VK_B, [ssMeta]);
29+```
30+
31+### ActionList use
32+
33+It is often helpful to use the Menu controls in conjunction with a [[TActionList]] which contains a series of standard or customised [[TAction|TActions]]. Menu items can be linked in the Object Inspector to *actions* on the list, and the same actions can be linked to [[TButton|TButtons]], [[TToolButton|TToolButtons]], [[TSpeedButton|TSpeedButtons]] etc. It is obviously more efficient to re-use the same code to respond to the various events, rather than writing separate *OnClick* event handlers for each individual control.
34+
35+By default, a number of standard actions are pre-loaded from *StdActns* or, if DataAware controls are used, from *DBActns*. These actions can be chosen using the [[ActionList editor]] which appears when you right-click on the [[TActionList]] icon on the Form Designer.
36+
37+---
38+*From the Free Pascal wiki, page "How to use menu controls", as it stood on 2019-09-29 after a change by Djzepi, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history