Sign in Join
Wiki copytemporary

How to use menu controls: what changed

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

OldNewLine
11### TMainMenu
22
33[[TMainMenu]] is the Main Menu that appears at the top of most forms; form designers can customise by choosing various menu items.
44
55[[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.
66
77### TPopupMenu
88
99[[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.
1010
1111### Menu editor
1212
1313To 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.
1414
1515An 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.
1616
1717Any of the [[TMenuItem|TMenuItems]] that you add can be configured using the [[IDE Window: Object Inspector|Object Inspector]].
1818
1919At 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*.
2020
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]].
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 `Ctrl`+`C` for Copy or `Ctrl`+`V` for Paste - the standard keyboard shortcuts) with the *ShortCut* property of the [[TMenuItem]].
2222
2323#### Setting Shortcuts Programmatically
2424
2525Two functions are provided that convert virtual key to shortcuts and visa versa, KeyToShortCut() and ShortCutToKey(). eg
2626
2727``` pascal
2828MenuBold.ShortCut:= KeyToShortCut(VK_B, [ssMeta]);
2929```
3030
3131### ActionList use
3232
3333It 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.
3434
3535By 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.
3636
3737---
3838*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