This documentation is archived and is not being maintained.

Menu.CloneMenu Method

Copies the Menu that is passed as a parameter to the current Menu.

[Visual Basic]
Protected Sub CloneMenu( _
   ByVal menuSrc As Menu _
)
[C#]
protected void CloneMenu(
 Menu menuSrc
);
[C++]
protected: void CloneMenu(
 Menu* menuSrc
);
[JScript]
protected function CloneMenu(
   menuSrc : Menu
);

Parameters

menuSrc
The Menu to copy.

Remarks

This method copies the entire list of MenuItem objects (stored in the Menu passed in to menuSrc) into the current menu. You can use this method in your derived class to clone MenuItem objects. They can then be reused by other classes that derive from Menu, such as MainMenu, ContextMenu, and MenuItem.

Example

[Visual Basic, C#, C++] This example creates a main menu, mainMenu1, with three menu items, File, Edit, and View. It then uses the CloneMenu method to copy mainMenu1 into mainMenu2. The cloned menu is then assigned to the form and displayed. This program assumes that you have already created a Form named Form1.

[Visual Basic] 
Private Sub CloneMyMainMenu()
   ' Create the main menu.
   Dim mainMenu1 As New MainMenu()

   ' Create the menu items to add.
   Dim menuItem1 As New MenuItem()
   Dim menuItem2 As New MenuItem()
   Dim menuItem3 As New MenuItem()

   ' Set the caption for the menu items.
   menuItem1.Text = "File"
   menuItem2.Text = "Edit"
   menuItem3.Text = "View"

   ' Add the menu item to mainMenu1.
   mainMenu1.MenuItems.Add(menuItem1)
   mainMenu1.MenuItems.Add(menuItem2)
   mainMenu1.MenuItems.Add(menuItem3)

   ' Clone the mainMenu1 and name it mainMenu2.
   Dim mainMenu2 As MainMenu = mainMenu1.CloneMenu()

   ' Assign mainMenu2 to the form.
   Menu = mainMenu2
End Sub 'CloneMyMainMenu

[C#] 
private void CloneMyMainMenu()
{
    // Create the main menu.
    MainMenu mainMenu1 = new MainMenu();

    // Create the menu items to add.
    MenuItem menuItem1 = new MenuItem();
    MenuItem menuItem2 = new MenuItem();
    MenuItem menuItem3 = new MenuItem();
   
    // Set the caption for the menu items.
    menuItem1.Text = "File";
    menuItem2.Text = "Edit";
    menuItem3.Text = "View";

    // Add the menu item to mainMenu1.
    mainMenu1.MenuItems.Add(menuItem1);
    mainMenu1.MenuItems.Add(menuItem2);
    mainMenu1.MenuItems.Add(menuItem3);

    // Clone the mainMenu1 and name it mainMenu2.
    MainMenu mainMenu2 = mainMenu1.CloneMenu();
    
    // Assign mainMenu2 to the form.
    Menu = mainMenu2;
}

[C++] 
void CloneMyMainMenu()
{
    // Create the main menu.
    MainMenu* mainMenu1 = new MainMenu();

    // Create the menu items to add.
    MenuItem* menuItem1 = new MenuItem();
    MenuItem* menuItem2 = new MenuItem();
    MenuItem* menuItem3 = new MenuItem();

    // Set the caption for the menu items.
    menuItem1->Text = S"File";
    menuItem2->Text = S"Edit";
    menuItem3->Text = S"View";

    // Add the menu item to mainMenu1.
    mainMenu1->MenuItems->Add(menuItem1);
    mainMenu1->MenuItems->Add(menuItem2);
    mainMenu1->MenuItems->Add(menuItem3);

    // Clone the mainMenu1 and name it mainMenu2.
    MainMenu* mainMenu2 = mainMenu1->CloneMenu();

    // Assign mainMenu2 to the form.
    Menu = mainMenu2;
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Menu Class | Menu Members | System.Windows.Forms Namespace | MenuItem | MainMenu

Show: