MenuItem.Parent 属性

定义

获取一个值,该值指示包含此菜单项的菜单。Gets a value indicating the menu that contains this menu item.

public:
 property System::Windows::Forms::Menu ^ Parent { System::Windows::Forms::Menu ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.Menu Parent { get; }
member this.Parent : System.Windows.Forms.Menu
Public ReadOnly Property Parent As Menu

属性值

表示包含此菜单项的菜单的 MenuA Menu that represents the menu that contains this menu item.

属性

示例

在此示例中,您将创建一个主菜单和一个顶级菜单项,menuItem1File)。In this example, you create a main menu and a top-level menu item, menuItem1 (File). 还会创建两个菜单项,menuItem2New)和 menuItem3Open),然后将它们添加到 menuItem1的菜单项列表。You also create two menu items, menuItem2 (New) and menuItem3 (Open), and add them to the menu item list of menuItem1. 然后,查看 menuItem3 是否有父菜单,该菜单为 true,并在 messagebox 中显示此父菜单的信息。Then you check to see if menuItem3 has got a parent menu, which is true, and display the information of this parent menu in message box. 此示例需要你创建了一个名为 Form1FormThis example requires that you have created a Form named Form1.

public:
   void CreateMyMenuItems()
   {
      // Craete a main menu object.
      MainMenu^ mainMenu1 = gcnew MainMenu;

      // Create three top-level menu items.
      MenuItem^ menuItem1 = gcnew MenuItem( "&File" );
      MenuItem^ menuItem2 = gcnew MenuItem( "&New" );
      MenuItem^ menuItem3 = gcnew MenuItem( "&Open" );

      // Add menuItem1 to the main menu.
      mainMenu1->MenuItems->Add( menuItem1 );

      // Add menuItem2 and menuItem3 to menuItem1.
      menuItem1->MenuItems->Add( menuItem2 );
      menuItem1->MenuItems->Add( menuItem3 );

      // Check to see if menuItem3 has a parent menu.
      if ( menuItem3->Parent != nullptr )
            MessageBox::Show( String::Concat( menuItem3->Parent, "." ), "Parent Menu Information of menuItem3" );
      else
            MessageBox::Show( "No parent menu." );

      // Assign mainMenu1 to the form.
      this->Menu = mainMenu1;
   }
public void CreateMyMenuItems()
{
    // Craete a main menu object.
    MainMenu mainMenu1 = new MainMenu();

    // Create three top-level menu items.
    MenuItem menuItem1 = new MenuItem("&File");
    MenuItem menuItem2 = new MenuItem("&New");
    MenuItem menuItem3 = new MenuItem("&Open");

    // Add menuItem1 to the main menu.
    mainMenu1.MenuItems.Add(menuItem1);	

    // Add menuItem2 and menuItem3 to menuItem1.
    menuItem1.MenuItems.Add(menuItem2);
    menuItem1.MenuItems.Add(menuItem3);

    // Check to see if menuItem3 has a parent menu.
    if (menuItem3.Parent != null)
        MessageBox.Show(menuItem3.Parent.ToString()+
                ".", "Parent Menu Information of menuItem3"); 
    else
        MessageBox.Show("No parent menu."); 

    // Assign mainMenu1 to the form.
    this.Menu = mainMenu1;			
}
Public Sub CreateMyMenuItems()
    ' Craete a main menu object.
    Dim mainMenu1 As New MainMenu()

    ' Create three top-level menu items.
    Dim menuItem1 As New MenuItem("&File")
    Dim menuItem2 As New MenuItem("&New")
    Dim menuItem3 As New MenuItem("&Open")

    ' Add menuItem1 to the main menu.
    mainMenu1.MenuItems.Add(menuItem1)

    ' Add menuItem2 and menuItem3 to menuItem1.
    menuItem1.MenuItems.Add(menuItem2)
    menuItem1.MenuItems.Add(menuItem3)

    ' Check to see if menuItem3 has a parent menu.
    If (menuItem3.Parent IsNot Nothing) Then
        MessageBox.Show(menuItem3.Parent.ToString() + ".", "Parent Menu Information of menuItem3")
    Else
        MessageBox.Show("No parent menu.")
    End If
    ' Assign mainMenu1 to the form.
    Me.Menu = mainMenu1
End Sub

注解

您可以使用此属性来获取子菜单的 Menu 对象。You can use this property to obtain the Menu object for a submenu. 可以将此属性返回的 Menu 对象转换为 MenuItem 的对象,以对其进行操作。You can cast the Menu object returned by this property to a MenuItem object to manipulate it.

适用于