TMenuItem
Source: source/classes/menuitem.prg
Standalone class
TMenuItem represents a single entry within a TMenu object. Each item has a prompt, an optional action (code block), a checked/unchecked state, an enabled/disabled state, and a WHEN condition for dynamic visibility. Items can also act as separators or open submenus.
Key DATA Members
| DATA | Type | Description |
|---|---|---|
cPrompt | Character | Menu item text, with & for accelerator underline |
nId | Numeric | Command ID for the menu item |
bAction | Block | Code block executed on selection |
bWhen | Block | Code block evaluated to enable/disable item dynamically |
lChecked | Logical | Whether the item shows a check mark |
lActive | Logical | Whether the item is enabled (clickable) |
lSeparator | Logical | .T. for a separator bar |
oMenu | Object | Parent TMenu object containing this item |
Methods
| Method | Description |
|---|---|
New( cPrompt, cMsg, lChecked, lActive, bAction, bWhen, ... ) | Create a new menu item |
ReDefine( cPrompt, ... ) | Redefine a menu item from a resource |
SetCheck( lOn ) | Toggle the check mark on/off |
Enable() | Enable the menu item |
Disable() | Disable (gray out) the menu item |
SetPrompt( cText ) | Change the menu item text at runtime |
Example: Menu Item with Action and Check
#include "FiveWin.ch"
function Main()
local oWnd, oMenu, oItemStatus
DEFINE WINDOW oWnd TITLE "TMenuItem Demo" ;
MENU buildMenu( @oItemStatus )
ACTIVATE WINDOW oWnd CENTERED
return nil
function buildMenu( oItemStatus )
local oMenu
MENU oMenu
MENUITEM "&View"
MENU oSub
MENUITEM "&Toolbar" ACTION MsgInfo( "Toggle Toolbar" )
MENUITEM "&Status Bar" ;
ACTION MsgInfo( "Toggle Status Bar" ) ;
CHECKED
SEPARATOR
MENUITEM "E&xit" ACTION oWnd:End()
ENDMENU
ENDMENU
return oMenu
Notes
- Use the
CHECKEDclause in theMENUITEMcommand to create an item that is initially checked. - The
bWhenblock is evaluated each time the menu is opened. If it returns .F., the item is disabled. SetPrompt()allows dynamic menu text changes, useful for recent-file lists or state indicators.- Separator items are created with the
SEPARATORcommand; they have no action or prompt. - Each menu item is assigned a unique
nIdby the parent menu, used for command routing and accelerator mapping.