Difference between revisions of "How to use menu controls"

From Lazarus wiki
Jump to navigationJump to search
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Hints for creating menus for your forms.
+
<noinclude>{{How to use menu controls}} </noinclude>
  
 
===TMainMenu===
 
===TMainMenu===
 
[[TMainMenu]] is the Main Menu that appears at the top of most forms; form designers can customise by choosing various menu items.  
 
[[TMainMenu]] is the Main Menu that appears at the top of most forms; form designers can customise by choosing various menu items.  
  
[[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 run time. Instead, a menu bar with a structure defined by the Menu Editor will appear.  
+
[[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.  
  
 
===TPopupMenu===
 
===TPopupMenu===
[[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 of that component, thus providing a context sensitive menu for that component.
+
[[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.
  
 
===Menu editor ===
 
===Menu editor ===
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.
+
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.
  
 
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.
 
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.
Line 18: Line 18:
 
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''.
 
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 {{keypress|Ctrl}}+{{keypress|C}} for Copy or {{keypress|Ctrl}}+{{keypress|V}} for Paste - the standard keyboard shortcuts) with the ''ShortCut'' property of the [[TMenuItem]].
+
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 {{keypress|Ctrl}}+{{keypress|C}} for Copy or {{keypress|Ctrl}}+{{keypress|V}} for Paste - the standard keyboard shortcuts) with the ''ShortCut'' property of the [[TMenuItem]].
 +
 
 +
====Setting Shortcuts Programmatically====
 +
Two functions are provided that convert virtual key to shortcuts and visa versa, KeyToShortCut() and ShortCutToKey(). eg
 +
 
 +
<syntaxhighlight lang="pascal">MenuBold.ShortCut:= KeyToShortCut(VK_B, [ssMeta]);</syntaxhighlight>
  
 
===ActionList use===
 
===ActionList use===

Latest revision as of 20:08, 29 September 2019

English (en) suomi (fi)

TMainMenu

TMainMenu is the Main Menu that appears at the top of most forms; form designers can customise by choosing various menu items.

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 run time. Instead, a menu bar with a structure defined by the Menu Editor will appear.

TPopupMenu

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 of that component, thus providing a context sensitive menu for that component.

Menu editor

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.

Setting Shortcuts Programmatically

Two functions are provided that convert virtual key to shortcuts and visa versa, KeyToShortCut() and ShortCutToKey(). eg

MenuBold.ShortCut:= KeyToShortCut(VK_B, [ssMeta]);

ActionList use

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.