KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Generic method to handle multiple menu items
PRODUCT: 4D | VERSION: 14.1 | PLATFORM: Mac & Win
Published On: July 22, 2014

A common practice for menus is to have methods attached to them. Methods for each menu item can be assigned from the toolbar.



Each menu item can run their own individual method, or run the same method. Having multiple menu items run the same method has the benefit of managing the functions of those menu items in a single method. Furthermore, it allows menu item methods to take in parameters. This could not be done if assigning method from the toolbox because there was no way to pass parameters to the method.

One method to handle what menu items do:

// Method: HandleMenuItem
// Description: Detects which menu item was selected and
//              runs the appropriate method based off the
//              selected menu item's name
//-----------------------------------------

C_TEXT($MenuItemName_t)

//Return Title of Menu Item
$MenuItemName_t:=(Get menu item(Menu selected\65536;Menu selected%65536))

//Determine method to run based of title of Menu Item
Case of
: ($MenuItemName_t:=”Item 1”)
   MyMenuMethod1
:($MenuItemName_t:=”Item 2”)
   MyMenuMethod2(“param1”)
:($MenuItemName_t:=”Item 3”)
   MyMenuMethod3(“param1”;”param2”)
End case


Using this setup will allow the user to edit all their menu methods inside one method, and determine what the menu item does based of the name/title of the menu item. This will also allow method called from menus to receive parameters.

In the toolbar, assign each menu item to run the method above (HandleMenuItem):