How to use menu controls

From Lazarus wiki
Revision as of 14:24, 22 March 2017 by Arent (talk | contribs)
Jump to navigationJump to search

Hints for creating menus for your forms

TMainMenu is the Main Menu that appears at the top of most forms; form designers can customise by choosing various menu items. TPopupMenu is a menu window that pops up with pertinent, usually context-sensitive, details and choices when the right mouse button is clicked near a control

Main Menu is a non-visible component: that is, if the icon is selected from the Component Palette and placed on the form, it will not appear at run time. Instead, a menu bar with a structure defined by the Menu Editor will appear. Popup menus (placed on the form by selecting the icon from the Component Palette) do not appear at all unless the right mouse button is clicked on a control that owns such a menu.

To see the 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.

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.

Any of the TMenuItems that you add can be configured using the Object Inspector.

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.

If you want a particular letter in the Caption to be associated with a shortcut key, that letter should be preceded by an ampersand (&). 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.

It is often helpful to use the Menu controls in conjunction with a TActionList which contains a series of standard or customised TActions. Menu items can be linked in the Object Inspector to actions on the list, and the same actions can be linked to TButtons, TToolButtons, 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.

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.