KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Building menu bars for component forms
PRODUCT: 4D | VERSION: 11 | PLATFORM: Mac & Win
Published On: April 29, 2010

When developing a component that is going to have forms presented for user interaction, a developer should not assign a menu bar to the form in the property list. This is because menu bars are not shared between components and hosts.

To have a menu bar available for a component form it must be built dynamically. Below is a method that can be used in a component that will install a menu bar with a functioning Edit menu when a component form window is opened. The sample code below is from a method named "COMP_Menus".

  `*****************************************************************************
   `//
   `// COMP_Menus
   `//
   `// Purpose: Set the component form menu bar with an active Edit menu
   `//
   `// $0 - TEXT - Return the MenuRef of the current menu bar
   `//
   `*****************************************************************************


C_TEXT($MethodName_T)
$MethodName_T:=Current method name

  `===================== Declare Variables ==================================
  `method_parameters_declarations
C_TEXT($0;$MBRef_T)

  `====================== Initialize and Setup ================================
$MBRef_T:=Get menu bar reference

  `======================== Method Actions ==================================

   `// Code for creating the File menu
   `//    just a stub so the edit menu comes up in the right place
   `//    add menu items as needed

C_STRING(16;FileMenu_A16)
FileMenu_A16:=Create menu

   `// Code for creating the Edit menu
C_STRING(16;EditMenu_A16)
EditMenu_A16:=Create menu
APPEND MENU ITEM(EditMenu_A16;"Cut;Copy;Paste;-;Select all")
SET MENU ITEM SHORTCUT(EditMenu_A16;1;Character code("X"))
SET MENU ITEM PROPERTY(EditMenu_A16;1;Associated Standard Action ;Cut Action )

SET MENU ITEM SHORTCUT(EditMenu_A16;2;Character code("C"))
SET MENU ITEM PROPERTY(EditMenu_A16;2;Associated Standard Action ;Copy Action )

SET MENU ITEM SHORTCUT(EditMenu_A16;3;Character code("V"))
SET MENU ITEM PROPERTY(EditMenu_A16;3;Associated Standard Action ;Paste Action )

   `// - Separator item

SET MENU ITEM SHORTCUT(EditMenu_A16;5;Character code("A"))
SET MENU ITEM PROPERTY(EditMenu_A16;5;Associated Standard Action ;Select all Action )

   `// Code for creating and installing the menu bar
C_STRING(16;COMP_Menu_A16)
COMP_Menu_A16:=Create menu
INSERT MENU ITEM(COMP_Menu_A16;-1;Get indexed string(79;1);FileMenu_A16)
APPEND MENU ITEM(COMP_Menu_A16;"Edit";EditMenu_A16)
SET MENU BAR(COMP_Menu_A16;Current process)

   `======================== Clean up and Exit =================================

   `// Return the reference for the preexisting menu bar

$0:=$MBRef_T


The sample above only installs a useful Edit Menu. The code can be expanded to include other menus and functionality. Below is an example of how the above method ("COMP_Menus") would be used within a component method that opens a user interaction window.

C_TEXT($CurrentMB_T)
C_LONGINT($Ref)

$CurrentMB_T:=COMP_Menus

$Ref:=Open form window("aForm";Plain form window;Horizontally Centered;Vertically Centered)
DIALOG("aForm")
CLOSE WINDOW($Ref)

   `// The process var COMP_Menu_A16 is declared in the method COMP_Menus
RELEASE MENU(COMP_Menu_A16)

If ($CurrentMB_T="")
    SET MENU BAR(1)
Else
    SET MENU BAR($CurrentMB_T)
End if