Menu.MergeMenu(Menu) Menu.MergeMenu(Menu) Menu.MergeMenu(Menu) Menu.MergeMenu(Menu) Method

定义

将一个菜单的 MenuItem 对象与当前菜单合并。Merges the MenuItem objects of one menu with the current menu.

public:
 virtual void MergeMenu(System::Windows::Forms::Menu ^ menuSrc);
public virtual void MergeMenu (System.Windows.Forms.Menu menuSrc);
abstract member MergeMenu : System.Windows.Forms.Menu -> unit
override this.MergeMenu : System.Windows.Forms.Menu -> unit
Public Overridable Sub MergeMenu (menuSrc As Menu)

参数

menuSrc
Menu Menu Menu Menu

其菜单项与当前菜单的菜单项合并的 MenuThe Menu whose menu items are merged with the menu items of the current menu.

异常

它已尝试将菜单与自己合并在一起。It was attempted to merge the menu with itself.

示例

此示例将创建两个菜单mainMenu1mainMenu2This example creates two menus, mainMenu1 and mainMenu2. 第一个菜单包含一个MenuItemFile,并且第二个包含一个MenuItemEditThe first menu contains one MenuItem, File, and the second contains one MenuItem, Edit. 使用MergeMenu方法,mainMenu2与合并mainMenu1Using the MergeMenu method, mainMenu2 is merged with mainMenu1. 显示时,mainMenu1将包含两个菜单项FileEditWhen displayed, mainMenu1 will contain the two menu items, File and Edit. 此示例要求您已创建Form名为Form1This example requires that you have created a Form named Form1.

private:
   void InitializeMyMainMenu()
   {
      // Create the 2 menus and the menu items to add.
      MainMenu^ mainMenu1 = gcnew MainMenu;
      MainMenu^ mainMenu2 = gcnew MainMenu;
      MenuItem^ menuItem1 = gcnew MenuItem;
      MenuItem^ menuItem2 = gcnew 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;
   }
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;
}
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

注解

此方法将合并MenuItem一个与当前菜单的菜单中的对象。This method merges MenuItem objects from one menu with the current menu. MenuItem 对象的值根据合并MenuItem.MergeTypeMenuItem.MergeOrder属性。MenuItem objects are merged according to the values of the MenuItem.MergeType and MenuItem.MergeOrder properties.

菜单合并功能通常是为了合并与其活动的 MDI 子窗体多文档界面 (MDI) 父窗体的菜单。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. 这是由.NET Framework 公共语言运行时自动执行。This is performed automatically by the .NET Framework common language runtime. 例如,如果 MDI 父窗体包含一组用于处理文件的菜单和 MDI 子窗体还具有与文件相关的菜单项,菜单集将自动合并到单个文件菜单的子窗体显示在 MDI 父窗体时设置。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.

可以使用MergeMenu如果要合并两个不属于 MDI 应用程序的菜单结构的方法。You can use the MergeMenu method if you are merging two menu structures that are not part of an MDI application. 此实现可以调用MainMenuContextMenu类,以合并两个或多个派生的对象MenuThis implementation can be called by the MainMenu or ContextMenu classes to merge two or more objects that derive from Menu.

合并两个MenuItem对象,请使用MenuItem.MergeMenu方法。To merge two MenuItem objects use the MenuItem.MergeMenu method.

适用于

另请参阅