MenuItem.Parent 属性

定义

获取一个值,该值指示包含此菜单项的菜单。

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

属性值

表示包含此菜单项的菜单的 Menu

属性

示例

在此示例中,创建主菜单和顶级菜单项, menuItem1 (File) 。 还可以创建两个菜单项, menuItem2 (New) 和 menuItem3 (Open) ,并将其添加到 的 menuItem1菜单项列表中。 然后检查是否有 menuItem3 父菜单(为 true),并在消息框中显示此父菜单的信息。 此示例要求已创建名为 FormForm1

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 子菜单的 对象。 可以将此属性返回的对象强制转换为 MenuMenuItem 对象以对其进行操作。

适用于