From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!

MenuItem.Clone() As MenuItem

New in 2011r2

Supported for all project types and targets.

Makes a copy of the MenuItem and its children if any. Mac does not permit duplicate MenuItems, so you will need to create clones of any MenuItems that are now being used in two or more locations.


The clone contains only new objects so it does not share any reference with the original MenuItem. This is especially useful for Mac applications because the Mac framework does not allow the same MenuItem to be used in different places, e.g. in several MenuBars. If you do so, a MenuHasParentException is raised. To avoid the problem, remove duplicate MenuItems and create clones of the original MenuItem instead.

Clone and MenuItem Subclasses

If you intend to clone an instance of a MenuItem subclass, you should overload the copy constructor otherwise the values of your subclass properties will not be cloned. Suppose you have a MenuItem subclass CustomMenuItem, you can implement a copy Constructor like this:

The constructor copies the passed CustomMenuItem, and then copies the values of CustomMenuItem properties.

Sub Constructor(copy As CustomMenuItem)

// Assign your custom parameters
Self.Foo = copy.Foo
End Sub

This constructor now allows the Clone method to successfully copies the custom properties:

Var origMenu As New CustomMenuItem()
origMenu.Foo = 12

Var copyMenu As MenuItem = origMenu.Clone

and get a copy of item, including all CustomMenuItem properties.

Sample Code

Whenever you need to use the same MenuItem in different places, use the Clone method to create a new copy of the MenuItem and its children, if any.

Var mi As New MenuItem("My menu item")
Var myClone As MenuItem

myClone = mi.Clone // Creates an independent copy of "mi"

See Also

MenuHasParentException exception.