Form.Menu Form.Menu Form.Menu Form.Menu Property

定义

获取或设置在窗体中显示的 MainMenuGets or sets the MainMenu that is displayed in the form.

public:
 property System::Windows::Forms::MainMenu ^ Menu { System::Windows::Forms::MainMenu ^ get(); void set(System::Windows::Forms::MainMenu ^ value); };
[System.ComponentModel.Browsable(false)]
[System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ReferenceConverter))]
public System.Windows.Forms.MainMenu Menu { get; set; }
member this.Menu : System.Windows.Forms.MainMenu with get, set
Public Property Menu As MainMenu

属性值

MainMenu,表示要在窗体中显示的菜单。A MainMenu that represents the menu to display in the form.

示例

下面的代码示例将创建MainMenu,将分配两个MenuItem对象添加到MainMenu并将其绑定到窗体。The following code example creates a MainMenu, assigns two MenuItem objects to the MainMenu and binds it to a form. 此示例要求具有Form创建名为Form1This example requires that you have a Form created that is named Form1.

void CreateMyMainMenu()
{
   // Create an empty MainMenu.
   MainMenu^ mainMenu1 = gcnew MainMenu;
   MenuItem^ menuItem1 = gcnew MenuItem;
   MenuItem^ menuItem2 = gcnew MenuItem;
   menuItem1->Text = "File";
   menuItem2->Text = "Edit";

   // Add two MenuItem objects to the MainMenu.
   mainMenu1->MenuItems->Add( menuItem1 );
   mainMenu1->MenuItems->Add( menuItem2 );

   // Bind the MainMenu to Form1.
   Menu = mainMenu1;
}
public void CreateMyMainMenu()
{
   // Create an empty MainMenu.
   MainMenu mainMenu1 = new MainMenu();

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

   menuItem1.Text = "File";
   menuItem2.Text = "Edit";
   // Add two MenuItem objects to the MainMenu.
   mainMenu1.MenuItems.Add(menuItem1);
   mainMenu1.MenuItems.Add(menuItem2);
   
   // Bind the MainMenu to Form1.
   Menu = mainMenu1;   
}

Public Sub CreateMyMainMenu()
    ' Create an empty MainMenu.
    Dim mainMenu1 As New MainMenu()
       
    Dim menuItem1 As New MenuItem()
    Dim menuItem2 As New MenuItem()
       
    menuItem1.Text = "File"
    menuItem2.Text = "Edit"
    ' Add two MenuItem objects to the MainMenu.
    mainMenu1.MenuItems.Add(menuItem1)
    mainMenu1.MenuItems.Add(menuItem2)
       
    ' Bind the MainMenu to Form1.
    Menu = mainMenu1
End Sub

注解

此属性可用于在运行时完成菜单集之间切换。You can use this property to switch between complete menu sets at run time. 例如,定义MainMenu多文档界面 (MDI) 窗体具有没有活动的 MDI 子窗体,另一个时要显示MainMenu显示的子窗口时显示。For example, you can define one MainMenu to be displayed when your multiple-document interface (MDI) form has no active MDI child forms and another MainMenu to display when a child window is displayed. 您还可以使用其他MainMenu的应用程序需要显示不同的菜单组中存在特定条件时。You can also use a different MainMenu when specific conditions exist in your application that require displaying a different menu set.

适用于

另请参阅