Runtime menus are created dynamically by the application, as they are required.
The runtime menu may list user-selected information, recently used files, or a
list of Internet Web sites, to name three examples.
To create runtime menus dynamically, you must use a menu control array. At
runtime the Load statement creates the new menu items, and the Unload statement
removes them.
The following sections detail how runtime menus are created and removed from
the menu system.
Creating Runtime Menu Items
The three steps to creating a runtime menu item are as follows:
STEP BY STEP
3.1 Creating a Runtime Menu Item
-
Create a design time menu that will act as the template, as shown in Figure
3.8. Set the Index property of the template item to create a menu control array.
This array will allow new elements to be created at runtime. These elements can
then be controlled like a regular menu item.

FIGURE 3.8 - Creating a template menu item with an index value.
-
Use the Load statement at runtime. The Load statement accepts the name of
the object to be loaded. The following is sample syntax: Load mnuFileItem(1)
The name of the menu item is based on the design time item that had the Index
property set. To create a new menu, just use the menu name and a unique number
that has not been previously used in the array.
-
After the control has been loaded, use the menu name and the index value to
refer to the new menu. The menu item can then have all the normal properties of
menus set at runtime.
Dynamically created menu items appear directly under the preceding index item.
This must be taken into consideration when incorporating menu items below the
array. Menu items below the array will function as expected; however, as the new
elements are added to the collection, regular menu items appear lower on the menu.
Compare the positions of the Exit menu item on the File menu before and after
dynamic menu items have been added, as shown in Figures 3.9 and 3.10.


Code for Runtime Menu Items
When a menu item has been created at runtime, it is part of a control array.
When code is to be associated with the runtime-generated menu, just use the design
time menu item that was the first index number in the array.
The template menu item will have an extra argument in the
Click event. The Index argument provides the number used to refer to that
control. The following sample code demonstrates one way to code for the dynamic
menus:
Sub mnuFileItem_Click(Index as Integer)
Select Case Index
Case 0
MsgBox "You clicked the first menu item!"
Case 1
MsgBox "You clicked the first dynamically
created menu item!"
Case 2
MsgBox "You clicked the second dynamically
created menu item!"
End Select
End Sub
Of course, code such as the above would be appropriate only when you knew ahead
of time exactly which items you would be adding to your menu at runtime and how
many maximum items there would be.
Removing Runtime Menu Items
You can use two different methods to remove the runtime menus. The first is
to hide the newly created item; the second is to unload it. When hiding a menu
item, the user interface will no longer display the item; however, program code
can still use the menu and control the properties.
mnuFileItem(1).Visible = False
If a runtime menu item is unloaded, that control and the associated properties
will be removed from memory. If required again, they will have to be loaded.
Unload mnuFileItem(1)
Only runtime control names and elements can be passed to the Unload statement.
If a design time menu item is passed to Unload, an application error will occur
because you can't unload controls created at design time.