DesktopMenuItem.Clone

From Xojo Documentation

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

DesktopMenuItem.Clone() As DesktopMenuItem

New in 2021r3

Supported for all project types and targets.

Makes a copy of the DesktopMenuItem 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.

Notes

The clone contains only new objects so it does not share any reference with the original DesktopMenuItem. This is especially useful for Mac applications because the Mac framework does not allow the same DesktopMenuItem to be used in different places, e.g. in several DesktopMenuBars. 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 DesktopMenuItem 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)
Super.Constructor(copy)

// 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 DesktopMenuItem = origMenu.Clone

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

Sample Code

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

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

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

See Also

MenuHasParentException exception.