Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
MenuItem Class
MenuItem Properties
 Parent Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
MenuItem.Parent Property

Gets a value indicating the menu that contains this menu item.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Visual Basic (Declaration)
Public ReadOnly Property Parent As Menu
Visual Basic (Usage)
Dim instance As MenuItem
Dim value As Menu

value = instance.Parent
C#
public Menu Parent { get; }
C++
public:
property Menu^ Parent {
    Menu^ get ();
}
J#
/** @property */
public Menu get_Parent ()
JScript
public function get Parent () : Menu

Property Value

A Menu that represents the menu that contains this menu item.

You can use this property to obtain the Menu object for a submenu. You can cast the Menu object returned by this property to a MenuItem object to manipulate it.

In this example, you create a main menu and a top-level menu item, menuItem1 (File). You also create two menu items, menuItem2 (New) and menuItem3 (Open), and add them to the menu item list of menuItem1. 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. This example requires that you have created a Form named Form1.

Visual Basic
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 Not (menuItem3.Parent Is 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 'CreateMyMenuItems
C#
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;            
}
C++
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;
   }
J#
    // 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.get_MenuItems().Add(menuItem1);
    // Add menuItem2 and menuItem3 to menuItem1.
    menuItem1.get_MenuItems().Add(menuItem2);
    menuItem1.get_MenuItems().Add(menuItem3);
    // Check to see if menuItem3 has a parent menu.
    if (menuItem3.get_Parent() != null) {
        MessageBox.Show(menuItem3.get_Parent().ToString() 
            + ".", "Parent Menu Information of menuItem3");
    }
    else {
        MessageBox.Show("No parent menu.");
    }
    // Assign mainMenu1 to the form.
    this.set_Menu(mainMenu1);
} //CreateMyMenuItems

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker