Create Cascading Submenus

To add a cascading submenu, create a submenu using the AddSubmenu method. This method creates a new PopupMenuItem object and adds it to the designated menu. This special kind of PopupMenuItem object is assigned the type of acSubmenu.

The AddSubmenu method takes three parameters as input: Index, Label, and Tag.

The Index parameter is an integer that specifies the position of the new menu item within the menu. The index begins with position zero (0) as the first position on the menu after the title. To add the new menu item to the end of a menu, set the Index parameter equal to the Count property of the menu. (The Count property of the menu represents the total number of menu items on that menu.)

The Label parameter is a string that defines the content and formatting of menu items. The text that the user sees displayed for the menu item is called the Caption, and it is derived from the label by interpreting all the DIESEL string expressions and special codes contained in the label. For example, the label “&Edit” produces the caption “Edit.”

The Tag parameter, or name tag, is a string consisting of alphanumeric and underscore (_) characters. This string uniquely identifies the menu item within a given menu.

The AddSubmenu method does not return the PopupMenuItem object that it creates. Instead, it returns the new menu that the submenu points to. The new menu, which is returned as a PopupMenu object, can then be populated as a normal menu would be. For information on populating a menu, see Add New Menu Items to a Menu.

Create and populate a submenu

This example creates a new menu called “TestMenu” and adds it to a submenu called “OpenFile.” The submenu is then populated with a menu item called “Open,” which opens a drawing when executed. Finally, the menu is displayed on the menu bar.

Sub Ch6_AddASubMenu()
	Dim currMenuGroup As AcadMenuGroup
	Set currMenuGroup = ThisDrawing.Application.MenuGroups.Item(0)

	' Create the new menu
	Dim newMenu As AcadPopupMenu
	Set newMenu = currMenuGroup.Menus.Add("TestMenu")

	' Add the submenu
	Dim FileSubMenu As AcadPopupMenu
	Set FileSubMenu = newMenu.AddSubMenu("", "OpenFile")

	' Add a menu item to the sub menu
	Dim newMenuItem As AcadPopupMenuItem
	Dim openMacro As String
	' Assign the macro the VB equivalent of "ESC ESC _open "
	openMacro = Chr(3) + Chr(3) + "_open "

	Set newMenuItem = FileSubMenu.AddMenuItem _
						(newMenu.count + 1, "Open", openMacro)

	' Display the menu on the menu bar
	newMenu.InsertInMenuBar _
				 (ThisDrawing.Application.menuBar.count + 1)
End Sub