Introduction
I've seen many threads in the PowerBuilder newsgroups wherein
an individual asks if one can create dynamic menu items and another person answers
"No!". This document and its associated script snippets are intended to
debunk this "no dynamic menu item" myth once and for all. One is only
limited by one's time and imagination, so I took my time and used my imagination to come
up with a straightforward, workable methodology for using only PB functionality to
implement dynamic menu items.
Objects
The first object you must create is a menu object with one menu
item. I named my menu object m_dynamic and the item m_dynamic_item. As part of
the menu object definition, we will create accessor and assignment functions, the source
of which can be found in the associated zip file. In short, we will use fmSetText()
to change the menu item's text to the desired string, fmSetID() to assign a unique ID to
each dynamic menu item (more about the ID later) and fmGetID() to obtain a dynamic menu
items .
The second object we will create is a non-visual object (NVO)
to manage our dynamic menu items. I named my manager nvo_dyn_menu_item_mgr.
This NVO will be created as a global (fear not - it has a small memory footprint so you'll
hardly notice it) and will serve to create and destroy your dynamic items. Let's
discuss at a high level how nvo_dyn_menu_item_mgr operates (you can check the scripts I
provide for the function names and implementation). When you ask it to create a
dynamic menu item, nvo_dyn_menu_item_mgr creates an instance of m_dynamic (or a descendant
thereof) and stores that reference in an instance array of type m_dynamic. From
here, the functions to set the menu item's text and the item's ID are called. There
are two uses for the ID member of the m_dynamic object: 1) identification of an item for
removal; and 2)(possibly) handling a click on a given item [NOTE: There are basically two
ways one can handle the clicked event of the dynamic menu items: 1)put the code in
the clicked event of the menu object; or 2)have the clicked event call a function or
trigger an event in nvo_dyn_menu_item_mgr passing the item's ID as an argument and
handling the processing there. There are arguments for and against each method, so
I'll leave it for you to decide.] nvo_dyn_menu_item_mgr provides the functionality
for removing individual dynamic items as well as removing all items in one fell
swoop(which is usually only needed when closing your application). The scripts for
the functions necessary for creating and destroying your dynamic menu items are also
included in the zip file.
From the ten-mile-high conceptual level, that's all there is to
it. View the scripts I provided. I comment generously, so you should be able
to follow along easily. If you have any questions, comments or suggestions, feel
free to e-mail me at twallace@email.com. Also, if you want to improve on this, feel
free, but please notify me of your improvements so I can see if they would benefit me as
well!
I hope this helps you in your PowerBuilder endeavors.