This documentation is archived and is not being maintained.

Menu.GetMainMenu Method

Gets the MainMenu that contains this menu.

[Visual Basic]
Public Function GetMainMenu() As MainMenu
[C#]
public MainMenu GetMainMenu();
[C++]
public: MainMenu* GetMainMenu();
[JScript]
public function GetMainMenu() : MainMenu;

Return Value

The MainMenu that contains this menu.

Remarks

This method allows you to obtain a reference to the MainMenu this menu is currently located in. This property returns a null reference (Nothing in Visual Basic) if the menu is not contained in a MainMenu. This can occur if the menu is contained in a MenuItem or ContextMenu, or if the menu is not contained in any menu. You can use this property to determine whether a menu is currently being used, and also to determine where.

Example

[Visual Basic, C#, C++] This example declares a MainMenu, mainMenu1, and four menu items, menuItem1, menuItem2, menuItem3, and menuItem4. Only the first three menu items are added to mainMenu1 for display. The program then checks to see if menuItem3 is being used, which is true, and determines the form in which it is used. A label text is used to display the name of the form that hosts the MainMenu. If you change the menu item being tested to menuItem4, the condition evaluates to false because menuItem4 has never beeen used. This example assumes that you have created a Form named MyForm that includes a Label named label1.

[Visual Basic] 
Private Sub InitializeMyMainMenu()
    ' Create the MainMenu and the menu items to add.
    Dim mainMenu1 As New MainMenu()

    Dim menuItem1 As New MenuItem()
    Dim menuItem2 As New MenuItem()
    Dim menuItem3 As New MenuItem()
    Dim menuItem4 As New MenuItem()


    ' Set the caption for the menu items.
    menuItem1.Text = "File"
    menuItem2.Text = "Edit"
    menuItem3.Text = "View"

    ' Add 3 menu items to the MainMenu for displaying.
    mainMenu1.MenuItems.Add(menuItem1)
    mainMenu1.MenuItems.Add(menuItem2)
    mainMenu1.MenuItems.Add(menuItem3)

    ' Assign mainMenu1 to the form.
    Menu = mainMenu1

    ' Check if menuItem3 is currently being used.
    If Not (menuItem3.GetMainMenu() Is Nothing) Then
        ' Display the name of the form in which it is located.
        Label1.Text = menuItem3.GetMainMenu().GetForm().ToString()
    End If
End Sub 'InitializeMyMainMenu 

[C#] 
private void InitializeMyMainMenu()
{
    // Create the MainMenu and the menu items to add.
    MainMenu mainMenu1 = new MainMenu();

    MenuItem menuItem1 = new MenuItem();
    MenuItem menuItem2 = new MenuItem();
    MenuItem menuItem3 = new MenuItem();
    MenuItem menuItem4 = new MenuItem();

   
    // Set the caption for the menu items.
    menuItem1.Text = "File";
    menuItem2.Text = "Edit";
    menuItem3.Text = "View";

    // Add 3 menu items to the MainMenu for displaying.
    mainMenu1.MenuItems.Add(menuItem1);
    mainMenu1.MenuItems.Add(menuItem2);
    mainMenu1.MenuItems.Add(menuItem3);

    // Assign mainMenu1 to the form.
    Menu = mainMenu1;

    // Check if menuItem3 is currently being used.
    if(menuItem3.GetMainMenu() != null)
        // Display the name of the form in which it is located.
        label1.Text= menuItem3.GetMainMenu().GetForm().ToString();
}

[C++] 
private:
    void InitializeMyMainMenu() {
        // Create the MainMenu and the menu items to add.
        MainMenu* mainMenu1 = new MainMenu();

        MenuItem* menuItem1 = new MenuItem();
        MenuItem* menuItem2 = new MenuItem();
        MenuItem* menuItem3 = new MenuItem();
        MenuItem* menuItem4 = new MenuItem();


        // Set the caption for the menu items.
        menuItem1->Text = S"File";
        menuItem2->Text = S"Edit";
        menuItem3->Text = S"View";

        // Add 3 menu items to the MainMenu for displaying.
        mainMenu1->MenuItems->Add(menuItem1);
        mainMenu1->MenuItems->Add(menuItem2);
        mainMenu1->MenuItems->Add(menuItem3);

        // Assign mainMenu1 to the form.
        Menu = mainMenu1;

        // Check if menuItem3 is currently being used.
        if (menuItem3->GetMainMenu() != 0)
            // Display the name of the form in which it is located.
            label1->Text= menuItem3->GetMainMenu()->GetForm()->ToString();
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Menu Class | Menu Members | System.Windows.Forms Namespace | GetContextMenu

Show: