Menu.GetMainMenu 方法

定义

获取包含该菜单的 MainMenu

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

返回

MainMenu

包含该菜单的 MainMenu

示例

本示例声明一个、mainMenu1四个MainMenu菜单项、menuItem1menuItem2menuItem3menuItem4。 仅将前三个菜单项添加到 mainMenu1 显示中。 然后,程序检查 menuItem3 是否正在使用,即 true,并确定所使用的窗体。 标签文本用于显示承载表单 MainMenu的名称。 如果更改要测试menuItem4的菜单项,则条件的计算结果false``menuItem4为从未使用过。 此示例要求你已创建一个Form包含Label命名名称的label1命名MyForm项。

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 当前所在的位置。 如果菜单不包含在一个 MainMenu. 中,则此属性返回 null 。 如果菜单包含在某个MenuItemContextMenu或菜单中,或者菜单未包含在任何菜单中,则可能会出现这种情况。 可以使用此属性来确定菜单当前是否正在使用,以及确定位置。

适用于

另请参阅