Modifica

Condividi tramite


MainMenu Constructors

Definition

Initializes a new instance of the MainMenu class.

Overloads

MainMenu()

Initializes a new instance of the MainMenu class without any specified menu items.

MainMenu(IContainer)

Initializes a new instance of the MainMenu class with the specified container.

MainMenu(MenuItem[])

Initializes a new instance of the MainMenu with a specified set of MenuItem objects.

MainMenu()

Initializes a new instance of the MainMenu class without any specified menu items.

public:
 MainMenu();
public MainMenu ();
Public Sub New ()

Examples

The following code example creates a MainMenu, assigns two MenuItem objects to the MainMenu and binds it to a form. This 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

Remarks

This version of the constructor creates a MainMenu without any specified MenuItem objects. To add menu items to the control use the other version of this constructor that accepts an array of MenuItem objects as its parameter or use the Add method of the MenuItems property.

See also

Applies to

MainMenu(IContainer)

Initializes a new instance of the MainMenu class with the specified container.

public:
 MainMenu(System::ComponentModel::IContainer ^ container);
public MainMenu (System.ComponentModel.IContainer container);
new System.Windows.Forms.MainMenu : System.ComponentModel.IContainer -> System.Windows.Forms.MainMenu
Public Sub New (container As IContainer)

Parameters

container
IContainer

An IContainer representing the container of the MainMenu.

Applies to

MainMenu(MenuItem[])

Initializes a new instance of the MainMenu with a specified set of MenuItem objects.

public:
 MainMenu(cli::array <System::Windows::Forms::MenuItem ^> ^ items);
public MainMenu (System.Windows.Forms.MenuItem[] items);
new System.Windows.Forms.MainMenu : System.Windows.Forms.MenuItem[] -> System.Windows.Forms.MainMenu
Public Sub New (items As MenuItem())

Parameters

items
MenuItem[]

An array of MenuItem objects that will be added to the MainMenu.

Examples

The following code example creates a MainMenu, and assigns two MenuItem objects to the MainMenu using this version of the constructor. The example then binds the MainMenu to a Form. This example requires that you have a Form created that is named Form1.

void CreateMyMainMenu()
{
   // Create two MenuItem objects and assign to array.
   MenuItem^ menuItem1 = gcnew MenuItem;
   MenuItem^ menuItem2 = gcnew MenuItem;
   menuItem1->Text = "&File";
   menuItem2->Text = "&Edit";

   // Create a MainMenu and assign MenuItem objects.
   array<MenuItem^>^temp2 = {menuItem1,menuItem2};
   MainMenu^ mainMenu1 = gcnew MainMenu( temp2 );

   // Bind the MainMenu to Form1.
   Menu = mainMenu1;
}
public void CreateMyMainMenu()
{
   // Create two MenuItem objects and assign to array.
   MenuItem menuItem1 = new MenuItem();
   MenuItem menuItem2 = new MenuItem();

   menuItem1.Text = "&File";
   menuItem2.Text = "&Edit";

   // Create a MainMenu and assign MenuItem objects.
   MainMenu mainMenu1 = new MainMenu(new MenuItem[] {
         menuItem1,
         menuItem2});
   
   // Bind the MainMenu to Form1.
   Menu = mainMenu1;   
}
Public Sub CreateMyMainMenu()
    ' Create two MenuItem objects and assign to array.
    Dim menuItem1 As New MenuItem()
    Dim menuItem2 As New MenuItem()
       
    menuItem1.Text = "&File"
    menuItem2.Text = "&Edit"
       
    ' Create a MainMenu and assign MenuItem objects.
    Dim mainMenu1 As New MainMenu(New MenuItem() {menuItem1, menuItem2})
       
    ' Bind the MainMenu to Form1.
    Menu = mainMenu1
End Sub

Remarks

You can use this constructor to assign an array of MenuItem objects to the MainMenu at the time of its creation. After the MainMenu has been created you can add additional MenuItem objects to the MainMenu using the Add method of the MenuItems property.

See also

Applies to