Export (0) Print
Expand All

Menu.MergeMenu Method

Merges the MenuItem objects of one menu with the current menu.

[Visual Basic]
Public Overridable Sub MergeMenu( _
   ByVal menuSrc As Menu _
)
[C#]
public virtual void MergeMenu(
 Menu menuSrc
);
[C++]
public: virtual void MergeMenu(
 Menu* menuSrc
);
[JScript]
public function MergeMenu(
   menuSrc : Menu
);

Parameters

menuSrc
The Menu whose menu items are merged with the menu items of the current menu.

Exceptions

Exception Type Condition
Exception The menu was merged with itself.

Remarks

This method merges MenuItem objects from one menu with the current menu. MenuItem objects are merged according to the values of the MenuItem.MergeType and MenuItem.MergeOrder properties.

Menu merging is typically done to merge the menus of a Multiple Document Interface (MDI) parent form with those of its active MDI child form. This is performed automatically by the .NET Framework common language runtime. For example, if an MDI parent form contains a set of menus for handling files and your MDI child form also has file-related menu items, the menu sets will automatically merge into a single file menu set when the child form is displayed in the MDI parent form.

You can use the MergeMenu method if you are merging two menu structures that are not part of an MDI application. This implementation can be called by the MainMenu or ContextMenu classes to merge two or more objects that derive from Menu.

To merge two MenuItem objects use the MenuItem.MergeMenu method.

Example

[Visual Basic, C#, C++] This example creates two menus, mainMenu1 and mainMenu2. The first menu contains one MenuItem, File, and the second contains one MenuItem, Edit. Using the MergeMenu method, mainMenu2 is merged with mainMenu1. When diplayed, mainMenu1 will contain the two menu items, File and Edit. This example assumes that you have created a Form named Form1.

[Visual Basic] 
Private Sub InitializeMyMainMenu()
    ' Create the 2 menus and the menu items to add.
    Dim mainMenu1 As New MainMenu()
    Dim mainMenu2 As New MainMenu()

    Dim menuItem1 As New MenuItem()
    Dim menuItem2 As New MenuItem()

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

    ' Add a menu item to each menu for displaying.
    mainMenu1.MenuItems.Add(menuItem1)
    mainMenu2.MenuItems.Add(menuItem2)

    ' Merge mainMenu2 with mainMenu1
    mainMenu1.MergeMenu(mainMenu2)

    ' Assign mainMenu1 to the form.
    Me.Menu = mainMenu1
End Sub 'InitializeMyMainMenu

[C#] 
private void InitializeMyMainMenu()
{
    // Create the 2 menus and the menu items to add.
    MainMenu mainMenu1 = new MainMenu();
    MainMenu mainMenu2 = new MainMenu();

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

    // Add a menu item to each menu for displaying.
    mainMenu1.MenuItems.Add(menuItem1);
    mainMenu2.MenuItems.Add(menuItem2);

    // Merge mainMenu2 with mainMenu1
    mainMenu1.MergeMenu(mainMenu2);

    // Assign mainMenu1 to the form.
    this.Menu = mainMenu1;
}

[C++] 
private:
    void InitializeMyMainMenu() {
        // Create the 2 menus and the menu items to add.
        MainMenu* mainMenu1 = new MainMenu();
        MainMenu* mainMenu2 = new MainMenu();

        MenuItem* menuItem1 = new MenuItem();
        MenuItem* menuItem2 = new MenuItem();

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

        // Add a menu item to each menu for displaying.
        mainMenu1->MenuItems->Add(menuItem1);
        mainMenu2->MenuItems->Add(menuItem2);

        // Merge mainMenu2 with mainMenu1
        mainMenu1->MergeMenu(mainMenu2);

        // Assign mainMenu1 to the form.
        this->Menu = mainMenu1;
    }

[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.MergeType | MenuItem.MergeOrder

Show:
© 2014 Microsoft