Menu.GetMainMenu 方法

定义

获取包含该菜单的 MainMenuGets the MainMenu that contains this menu.

public:
 System::Windows::Forms::MainMenu ^ GetMainMenu();
public System.Windows.Forms.MainMenu GetMainMenu ();
member this.GetMainMenu : unit -> System.Windows.Forms.MainMenu
Public Function GetMainMenu () As MainMenu

返回

包含该菜单的 MainMenuThe MainMenu that contains this menu.

示例

此示例声明一个 MainMenumainMenu1和四个菜单项,menuItem1menuItem2menuItem3menuItem4This example declares a MainMenu, mainMenu1, and four menu items, menuItem1, menuItem2, menuItem3, and menuItem4. 只有前三个菜单项添加到 mainMenu1 显示。Only the first three menu items are added to mainMenu1 for display. 然后,该程序将检查是否正在使用 menuItem3,这是 true的,并确定使用该程序的窗体。The program then checks to see if menuItem3 is being used, which is true, and determines the form in which it is used. 标签文本用于显示承载 MainMenu的窗体的名称。A label text is used to display the name of the form that hosts the MainMenu. 如果将所测试的菜单项更改 menuItem4,则条件的计算结果为 false,因为从未使用过 menuItem4If you change the menu item being tested to menuItem4, the condition evaluates to false because menuItem4 has never been used. 此示例要求你创建一个名为 MyFormForm,其中包含一个名为 label1LabelThis example requires that you have created a Form named MyForm that includes a Label named label1.

private:
   void InitializeMyMainMenu()
   {
      // Create the MainMenu and the menu items to add.
      MainMenu^ mainMenu1 = gcnew MainMenu;
      MenuItem^ menuItem1 = gcnew MenuItem;
      MenuItem^ menuItem2 = gcnew MenuItem;
      MenuItem^ menuItem3 = gcnew MenuItem;
      MenuItem^ menuItem4 = gcnew 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;

      // Determine whether menuItem3 is currently being used.
      if ( menuItem3->GetMainMenu() != nullptr )

      // Display the name of the form in which it is located.
      label1->Text = menuItem3->GetMainMenu()->GetForm()->ToString();
   }
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;

    // Determine whether 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();
}
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

    ' Determine whether menuItem3 is currently being used.
    If (menuItem3.GetMainMenu() IsNot Nothing) Then
        ' Display the name of the form in which it is located.
        Label1.Text = menuItem3.GetMainMenu().GetForm().ToString()
    End If
End Sub

注解

此方法允许你获取对该菜单当前所在 MainMenu 的引用。This method allows you to obtain a reference to the MainMenu this menu is currently located in. 如果菜单未包含在 MainMenu中,此属性将返回 nullThis property returns null if the menu is not contained in a MainMenu. 如果菜单包含在 MenuItemContextMenu中,或者如果菜单不包含在任何菜单中,则会发生这种情况。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.

适用于

另请参阅