ערוך

שתף באמצעות


MenuItem.CloneMenu Method

Definition

Creates a copy of a MenuItem.

Overloads

CloneMenu(MenuItem)

Creates a copy of the specified MenuItem.

CloneMenu()

Creates a copy of the current MenuItem.

CloneMenu(MenuItem)

Creates a copy of the specified MenuItem.

protected:
 void CloneMenu(System::Windows::Forms::MenuItem ^ itemSrc);
protected void CloneMenu (System.Windows.Forms.MenuItem itemSrc);
override this.CloneMenu : System.Windows.Forms.MenuItem -> unit
Protected Sub CloneMenu (itemSrc As MenuItem)

Parameters

itemSrc
MenuItem

The MenuItem that represents the menu item to copy.

Examples

The following code example initializes a new MenuItem with a copy of an existing MenuItem that is used in a MainMenu on a form. The code then adds the cloned MenuItem to a ContextMenu on the form. This example requires that a MenuItem is already created and named menuItem1 and that a ContextMenu control has been created and named contextMenu1.

public:
   void CloneMyMenu()
   {
      // Clone the existing MenuItem into the new MenuItem.
      MenuItem^ tempMenuItem = menuItem1->CloneMenu();
      
      // Assign the cloned MenuItem to the ContextMenu.
      contextMenu1->MenuItems->Add( tempMenuItem );
   }
public void CloneMyMenu()
{
   // Clone the existing MenuItem into the new MenuItem.
   MenuItem tempMenuItem = menuItem1.CloneMenu();
 
   // Assign the cloned MenuItem to the ContextMenu.
   contextMenu1.MenuItems.Add(tempMenuItem);
}
Public Sub CloneMyMenu()
    ' Clone the existing MenuItem into the new MenuItem.
    Dim tempMenuItem As MenuItem = menuItem1.CloneMenu()
       
    ' Assign the cloned MenuItem to the ContextMenu.
    contextMenu1.MenuItems.Add(tempMenuItem)
End Sub

Remarks

Call this method to create copies of menu items that you have already created for use in a shortcut menu or another menu structure within your application. This version of CloneMenu allows you to specify a specific MenuItem to copy instead of the menu item that is calling the method. You can use this method to initialize a new MenuItem object with a copy of another MenuItem. When a menu item is cloned, any event handlers specified in the original menu item will continue to function in the cloned version of the menu item. For example, if you created a MenuItem and connected its Click event to an event handler. When the menu item is cloned, the cloned menu item will call the same event handler.

Applies to

CloneMenu()

Creates a copy of the current MenuItem.

public:
 virtual System::Windows::Forms::MenuItem ^ CloneMenu();
public virtual System.Windows.Forms.MenuItem CloneMenu ();
override this.CloneMenu : unit -> System.Windows.Forms.MenuItem
Public Overridable Function CloneMenu () As MenuItem

Returns

A MenuItem that represents the duplicated menu item.

Examples

The following code example clones a MenuItem and displays it in a ContextMenu. The example requires that a MenuItem exists in a MainMenu on a form with the name of menuItem1 and that a ContextMenu object named contextMenu1 also exists on the form.

public:
   void CloneMyMenu()
   {
      // Clone the menu item and add it to the collection for the shortcut menu.
      contextMenu1->MenuItems->Add( menuItem1->CloneMenu() );
   }
public void CloneMyMenu()
{
   // Clone the menu item and add it to the collection for the shortcut menu.
   contextMenu1.MenuItems.Add(menuItem1.CloneMenu());
}
Public Sub CloneMyMenu()

    ' Clone the menu item and add it to the collection for the shortcut menu.
    contextMenu1.MenuItems.Add(menuItem1.CloneMenu())
    
End Sub

Remarks

MenuItem objects cannot be used in more than one place unless you obtain a copy of the MenuItem. You can call this method to create a copy of this menu item for use in a ContextMenu, MainMenu, or other MenuItem within your application. When a menu item is cloned, any event handlers specified in the original menu item will continue to function in the cloned version of the menu item. For example, if you created a MenuItem and connected its Click event to an event handler. When the menu item is cloned, the cloned menu item will call the same event handler.

Applies to