Form.Menu 属性

定义

获取或设置在窗体中显示的 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 example creates a MainMenu, assigns two MenuItem objects to the MainMenu and binds it to a form. 此示例要求创建一个名为 Form1FormThis 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. 如果应用程序中存在需要显示不同菜单集的特定条件,则还可以使用不同的 MainMenuYou can also use a different MainMenu when specific conditions exist in your application that require displaying a different menu set.

适用于

另请参阅