MenuItem.MergeOrder 属性

定义

获取或设置一个值,通过该值指示菜单项与另一个项合并时的相对位置。

public:
 property int MergeOrder { int get(); void set(int value); };
public int MergeOrder { get; set; }
member this.MergeOrder : int with get, set
Public Property MergeOrder As Integer

属性值

Int32

表示该菜单项的合并顺序位置的从零开始的索引。 默认值为 0。

示例

下面的代码示例演示如何使用 MergeOrder 属性来控制合并菜单的显示方式。 这是一个完整的示例,可在将它添加到项目后运行。

// The following code example demonstrates using the MenuItem 
// Merge-Order property to control the way a merged menu is displayed.
using namespace System::Windows::Forms;

//Declare a MainMenu object and its items.
public ref class Form1: public System::Windows::Forms::Form
{
public private:
   System::Windows::Forms::MainMenu^ mainMenu1;
   System::Windows::Forms::MenuItem^ fileItem;
   System::Windows::Forms::MenuItem^ newItem;
   System::Windows::Forms::MenuItem^ openItem;
   System::Windows::Forms::MenuItem^ saveItem;
   System::Windows::Forms::MenuItem^ optionsMenu;
   System::Windows::Forms::MenuItem^ viewItem;
   System::Windows::Forms::MenuItem^ toolsItem;

   // Declare a ContextMenu object and its items.
   System::Windows::Forms::ContextMenu^ contextMenu1;
   System::Windows::Forms::MenuItem^ cutItem;
   System::Windows::Forms::MenuItem^ copyItem;
   System::Windows::Forms::MenuItem^ pasteItem;

public:
   Form1()
      : Form()
   {
      this->mainMenu1 = gcnew System::Windows::Forms::MainMenu;
      this->fileItem = gcnew System::Windows::Forms::MenuItem;
      this->newItem = gcnew System::Windows::Forms::MenuItem;
      this->openItem = gcnew System::Windows::Forms::MenuItem;
      this->saveItem = gcnew System::Windows::Forms::MenuItem;
      this->viewItem = gcnew System::Windows::Forms::MenuItem;
      this->toolsItem = gcnew System::Windows::Forms::MenuItem;
      this->optionsMenu = gcnew System::Windows::Forms::MenuItem;
      this->toolsItem = gcnew System::Windows::Forms::MenuItem;
      this->viewItem = gcnew System::Windows::Forms::MenuItem;
      this->contextMenu1 = gcnew System::Windows::Forms::ContextMenu;
      this->cutItem = gcnew System::Windows::Forms::MenuItem;
      this->copyItem = gcnew System::Windows::Forms::MenuItem;
      this->pasteItem = gcnew System::Windows::Forms::MenuItem;
      
      //Add file menu item and options menu item to the MainMenu.
      array<System::Windows::Forms::MenuItem^>^temp0 = {this->fileItem,this->optionsMenu};
      this->mainMenu1->MenuItems->AddRange( temp0 );
      
      // Initialize the file menu and its contents.
      this->fileItem->Index = 0;
      this->fileItem->Text = "File";
      this->newItem->Index = 0;
      this->newItem->Text = "New";
      this->openItem->Index = 1;
      this->openItem->Text = "Open";
      this->saveItem->Index = 2;
      this->saveItem->Text = "Save";
      
      // Set the merge order of fileItem to 2 so it has a lower priority 
      // on the merged menu.
      this->fileItem->MergeOrder = 2;
      
      //Add the new items to the fileItem menu item collection.
      array<MenuItem^>^temp1 = {this->newItem,this->openItem,this->saveItem};
      this->fileItem->MenuItems->AddRange( temp1 );
      
      // Initialize the optionsMenu item and its contents.
      this->optionsMenu->Index = 1;
      this->optionsMenu->Text = "Options";
      this->viewItem->Index = 0;
      this->viewItem->Text = "View";
      this->toolsItem->Index = 1;
      this->toolsItem->Text = "Tools";
      
      // Set mergeOrder property to 1, so it has a higher priority than
      // the fileItem on the merged menu.
      this->optionsMenu->MergeOrder = 1;
      
      //Add view and tool items to the optionsItem menu item.
      array<MenuItem^>^temp2 = {this->viewItem,this->toolsItem};
      this->optionsMenu->MenuItems->AddRange( temp2 );
      
      // Initialize the menu items for the shortcut menu.
      this->cutItem->Index = 0;
      this->cutItem->Text = "Cut";
      this->cutItem->MergeOrder = 0;
      this->copyItem->Index = 1;
      this->copyItem->Text = "Copy";
      this->copyItem->MergeOrder = 0;
      this->pasteItem->Index = 2;
      this->pasteItem->Text = "Paste";
      this->pasteItem->MergeOrder = 0;
      
      // Add menu items to the shortcut menu.
      array<MenuItem^>^temp3 = {cutItem,copyItem,pasteItem};
      this->contextMenu1->MenuItems->AddRange( temp3 );
      
      // Add the mainMenu1 items to the shortcut menu as well, by
      // calling the MergeMenu method.
      contextMenu1->MergeMenu( mainMenu1 );
      
      //Initialize the form.
      this->ClientSize = System::Drawing::Size( 292, 266 );
      this->Name = "Form1";
      this->Text = "Right click on form for merged menu.";
      
      // Associate the event-handling method with the
      // MouseDown event.
      this->MouseDown += gcnew MouseEventHandler( this, &Form1::Form1_MouseDown );
      
      // Add mainMenu1 to the form.
      this->Menu = mainMenu1;
   }


private:
   void Form1_MouseDown( Object^ /*sender*/, MouseEventArgs^ e )
   {
      
      // Check for a right mouse click.
      if ( e->Button == ::MouseButtons::Right )
      {
         contextMenu1->Show( this, System::Drawing::Point( 30, 30 ) );
      }
   }

};


[System::STAThreadAttribute]
int main()
{
   Application::Run( gcnew Form1 );
}
// The following code example demonstrates using the MenuItem 
// Merge-Order property to control the way a merged menu is displayed.

using System.Windows.Forms;

public class Form1:
    System.Windows.Forms.Form

    //Declare a MainMenu object and its items.
{
    internal System.Windows.Forms.MainMenu mainMenu1;
    internal System.Windows.Forms.MenuItem fileItem;
    internal System.Windows.Forms.MenuItem newItem;
    internal System.Windows.Forms.MenuItem openItem;
    internal System.Windows.Forms.MenuItem saveItem;
    internal System.Windows.Forms.MenuItem optionsMenu;
    internal System.Windows.Forms.MenuItem viewItem;
    internal System.Windows.Forms.MenuItem toolsItem;

    // Declare a ContextMenu object and its items.
    internal System.Windows.Forms.ContextMenu contextMenu1;
    internal System.Windows.Forms.MenuItem cutItem;
    internal System.Windows.Forms.MenuItem copyItem;
    internal System.Windows.Forms.MenuItem pasteItem;

    public Form1() : base()
    {        
        this.mainMenu1 = new System.Windows.Forms.MainMenu();
        this.fileItem = new System.Windows.Forms.MenuItem();
        this.newItem = new System.Windows.Forms.MenuItem();
        this.openItem = new System.Windows.Forms.MenuItem();
        this.saveItem = new System.Windows.Forms.MenuItem();

        this.viewItem = new System.Windows.Forms.MenuItem();
        this.toolsItem = new System.Windows.Forms.MenuItem();

        this.optionsMenu = new System.Windows.Forms.MenuItem();
        this.toolsItem = new System.Windows.Forms.MenuItem();
        this.viewItem = new System.Windows.Forms.MenuItem();

        this.contextMenu1 = new System.Windows.Forms.ContextMenu();
        this.cutItem = new System.Windows.Forms.MenuItem();
        this.copyItem = new System.Windows.Forms.MenuItem();
        this.pasteItem = new System.Windows.Forms.MenuItem();

        //Add file menu item and options menu item to the MainMenu.
        this.mainMenu1.MenuItems.AddRange(
            new System.Windows.Forms.MenuItem[]
            {this.fileItem, this.optionsMenu});

        // Initialize the file menu and its contents.
        this.fileItem.Index = 0;
        this.fileItem.Text = "File";
        this.newItem.Index = 0;
        this.newItem.Text = "New";
        this.openItem.Index = 1;
        this.openItem.Text = "Open";
        this.saveItem.Index = 2;
        this.saveItem.Text = "Save";

        // Set the merge order of fileItem to 2 so it has a lower priority 
        // on the merged menu.
        this.fileItem.MergeOrder = 2;

        //Add the new items to the fileItem menu item collection.
        this.fileItem.MenuItems.AddRange(new MenuItem[]
            {this.newItem, this.openItem, this.saveItem});

        // Initialize the optionsMenu item and its contents.
        this.optionsMenu.Index = 1;
        this.optionsMenu.Text = "Options";

        this.viewItem.Index = 0;
        this.viewItem.Text = "View";
        this.toolsItem.Index = 1;
        this.toolsItem.Text = "Tools";

        // Set mergeOrder property to 1, so it has a higher priority than
        // the fileItem on the merged menu.
        this.optionsMenu.MergeOrder = 1;

        //Add view and tool items to the optionsItem menu item.
        this.optionsMenu.MenuItems.AddRange(new MenuItem[]
            {this.viewItem, this.toolsItem});

        // Initialize the menu items for the shortcut menu.
        this.cutItem.Index = 0;
        this.cutItem.Text = "Cut";
        this.cutItem.MergeOrder = 0;
        this.copyItem.Index = 1;
        this.copyItem.Text = "Copy";
        this.copyItem.MergeOrder = 0;
        this.pasteItem.Index = 2;
        this.pasteItem.Text = "Paste";
        this.pasteItem.MergeOrder = 0;

        // Add menu items to the shortcut menu.
        this.contextMenu1.MenuItems.AddRange(new MenuItem[]
            {cutItem, copyItem, pasteItem});

        // Add the mainMenu1 items to the shortcut menu as well, by
        // calling the MergeMenu method.
        contextMenu1.MergeMenu(mainMenu1);

        //Initialize the form.
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Name = "Form1";
        this.Text = "Right click on form for merged menu.";
        
        // Associate the event-handling method with the
        // MouseDown event.
        this.MouseDown +=new MouseEventHandler(Form1_MouseDown);

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

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {

        // Check for a right mouse click.
        if (e.Button==MouseButtons.Right)

            // Display a merged menu containing items from mainMenu1 
            // and contextMenu1.
        {
            contextMenu1.Show(this, new System.Drawing.Point(30, 30));
        }
    }

    [System.STAThreadAttribute]
    public static void Main()
    {
        Application.Run(new Form1());
    }
}
' The following code example demonstrates using the MenuItem 
' Merge-Order property to control the way a merged menu is displayed.



Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form

    'Declare a MainMenu object and its items.
    Friend WithEvents mainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents fileItem As System.Windows.Forms.MenuItem
    Friend WithEvents newItem As System.Windows.Forms.MenuItem
    Friend WithEvents openItem As System.Windows.Forms.MenuItem
    Friend WithEvents saveItem As System.Windows.Forms.MenuItem
    Friend WithEvents optionsMenu As System.Windows.Forms.MenuItem
    Friend WithEvents viewItem As System.Windows.Forms.MenuItem
    Friend WithEvents toolsItem As System.Windows.Forms.MenuItem

    ' Declare a ContextMenu object and its items.
    Friend WithEvents contextMenu1 As System.Windows.Forms.ContextMenu
    Friend WithEvents cutItem As System.Windows.Forms.MenuItem
    Friend WithEvents copyItem As System.Windows.Forms.MenuItem
    Friend WithEvents pasteItem As System.Windows.Forms.MenuItem

   
    Public Sub New()
        MyBase.New()
        Me.mainMenu1 = New System.Windows.Forms.MainMenu
        Me.fileItem = New System.Windows.Forms.MenuItem
        Me.newItem = New System.Windows.Forms.MenuItem
        Me.openItem = New System.Windows.Forms.MenuItem
        Me.saveItem = New System.Windows.Forms.MenuItem

        Me.viewItem = New System.Windows.Forms.MenuItem
        Me.toolsItem = New System.Windows.Forms.MenuItem


        Me.optionsMenu = New System.Windows.Forms.MenuItem
        Me.toolsItem = New System.Windows.Forms.MenuItem
        Me.viewItem = New System.Windows.Forms.MenuItem

        Me.contextMenu1 = New System.Windows.Forms.ContextMenu
        Me.cutItem = New System.Windows.Forms.MenuItem
        Me.copyItem = New System.Windows.Forms.MenuItem
        Me.pasteItem = New System.Windows.Forms.MenuItem

        'Add file menu item and options menu item to the MainMenu.
        Me.mainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() _
             {Me.fileItem, Me.optionsMenu})

        ' Initialize the file menu and its contents.
        Me.fileItem.Index = 0
        Me.fileItem.Text = "File"
        Me.newItem.Index = 0
        Me.newItem.Text = "New"
        Me.openItem.Index = 1
        Me.openItem.Text = "Open"
        Me.saveItem.Index = 2
        Me.saveItem.Text = "Save"


        ' Set the merge order of fileItem to 2 so it has a lower priority 
        ' on the merged menu.
        Me.fileItem.MergeOrder = 2

        'Add the new items to the fileItem menu item collection.
        Me.fileItem.MenuItems.AddRange(New MenuItem() _
           {Me.newItem, Me.openItem, Me.saveItem})
        '

        ' Initialize the optionsMenu item and its contents.
        Me.optionsMenu.Index = 1
        Me.optionsMenu.Text = "Options"

        Me.viewItem.Index = 0
        Me.viewItem.Text = "View"
        Me.toolsItem.Index = 1
        Me.toolsItem.Text = "Tools"

        ' Set mergeOrder property to 1, so it has a higher priority than
        ' the fileItem on the merged menu.
        Me.optionsMenu.MergeOrder = 1

        'Add view and tool items to the optionsItem menu item.
        Me.optionsMenu.MenuItems.AddRange _
            (New MenuItem() {Me.viewItem, Me.toolsItem})

        ' Initialize the menu items for the shortcut menu.
        Me.cutItem.Index = 0
        Me.cutItem.Text = "Cut"
        Me.cutItem.MergeOrder = 0
        Me.copyItem.Index = 1
        Me.copyItem.Text = "Copy"
        Me.copyItem.MergeOrder = 0
        Me.pasteItem.Index = 2
        Me.pasteItem.Text = "Paste"
        Me.pasteItem.MergeOrder = 0

        ' Add menu items to the shortcut menu.
        contextMenu1.MenuItems.AddRange _
            (New MenuItem() {cutItem, copyItem, pasteItem})

        ' Add the mainMenu1 items to the shortcut menu as well, by
        ' calling the MergeMenu method.
        contextMenu1.MergeMenu(mainMenu1)

        'Initialize the form.
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Name = "Form1"
        Me.Text = "Right click on form for merged menu."

        ' Add mainMenu1 to the form.
        Me.Menu = mainMenu1


    End Sub


    Private Sub Form1_MouseDown(ByVal sender As Object, _
        ByVal e As MouseEventArgs) Handles MyBase.MouseDown

        ' Check for a right mouse click.
        If (e.Button = MouseButtons.Right) Then

            ' Display a merged menu containing items from mainMenu1 
            ' and contextMenu1.
            contextMenu1.Show(Me, New System.Drawing.Point(30, 30))
        End If
    End Sub

    <System.STAThreadAttribute()> Public Shared Sub Main()
        Application.Run(New Form1)
    End Sub


    

End Class

注解

菜单项的合并顺序指定当包含的菜单结构与其他菜单结构 MenuItem 合并时,此菜单项将采用的相对位置。

适用于

另请参阅