MenuItem.CloneMenu 方法

定义

创建 MenuItem 的副本。Creates a copy of a MenuItem.

重载

CloneMenu(MenuItem)

创建指定 MenuItem 的副本。Creates a copy of the specified MenuItem.

CloneMenu()

创建当前 MenuItem 的副本。Creates a copy of the current MenuItem.

CloneMenu(MenuItem)

创建指定 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)

参数

itemSrc
MenuItem

表示要复制的菜单项的 MenuItemThe MenuItem that represents the menu item to copy.

示例

下面的代码示例使用在窗MenuItem体上使用的MainMenu现有MenuItem的副本初始化新的。The following code example initializes a new MenuItem with a copy of an existing MenuItem that is used in a MainMenu on a form. 然后,该代码将克隆MenuItem的添加ContextMenu到窗体上的。The code then adds the cloned MenuItem to a ContextMenu on the form. 此示例要求已创建MenuItem并命名menuItem1 ContextMenu了一个控件并且已创建并命名contextMenu1了该控件。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

注解

调用此方法以创建您已创建的菜单项的副本,以便在快捷菜单或应用程序中的其他菜单结构中使用。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. 使用此版本CloneMenu的可以指定要复制的MenuItem特定,而不是调用方法的菜单项。This version of CloneMenu allows you to specify a specific MenuItem to copy instead of the menu item that is calling the method. 您可以使用此方法来初始化具有其他MenuItem MenuItem副本的新对象。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. 例如,如果创建了MenuItem并将其Click事件连接到事件处理程序。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.

CloneMenu()

创建当前 MenuItem 的副本。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

返回

表示重复菜单项的 MenuItemA MenuItem that represents the duplicated menu item.

示例

下面的代码示例克隆MenuItem并将其显示ContextMenu在中。The following code example clones a MenuItem and displays it in a ContextMenu. MenuItem示例要求contextMenu1 MainMenu具有名称为menuItem1的窗体上存在,并且窗体上还存在名为的对象。ContextMenuThe 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

注解

MenuItem除非你获取的副本MenuItem,否则不能在多个位置中使用对象。MenuItem objects cannot be used in more than one place unless you obtain a copy of the MenuItem. 您可以调用此方法来创建此菜单项的副本ContextMenu,以便在应用程序中的、 MainMenuMenuItem其他项中使用。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. 例如,如果创建了MenuItem并将其Click事件连接到事件处理程序。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.

适用于