Export (0) Print
Expand All

MainMenu Class

Represents the menu structure of a form.

For a list of all members of this type, see MainMenu Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.Menu
            System.Windows.Forms.MainMenu

[Visual Basic]
Public Class MainMenu
   Inherits Menu
[C#]
public class MainMenu : Menu
[C++]
public __gc class MainMenu : public Menu
[JScript]
public class MainMenu extends Menu

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The MainMenu control represents the container for the menu structure of a form. A menu is composed of MenuItem objects that represent the individual menu commands in the menu structure. Each MenuItem can be a command for your application or a parent menu for other submenu items. To bind the MainMenu to the Form that will display it, assign the MainMenu to the Menu property of the Form.

For applications that will have support for multiple languages, you can use the RightToLeft property to display the text of the menu from right to left to support languages such as Arabic, which read from right to left.

You can create different MainMenu objects to represent different menu strutures for your form. If you want to reuse the menu structure contained in a specific MainMenu, you can use its CloneMenu method to create a copy. Once you have a copy of the menu structure, you can make the appropriate modifications for your new menu structure.

Example

[Visual Basic, C#, C++] The following example creates a MainMenu, assigns two MenuItem objects to the MainMenu and binds it to a form. This example assumes that you have a Form created that is named Form1.

[Visual Basic] 
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


[C#] 
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;   
}


[C++] 
public:
 void CreateMyMainMenu()
 {
    // Create an empty MainMenu.
    MainMenu* mainMenu1 = new MainMenu();
 
    MenuItem* menuItem1 = new MenuItem();
    MenuItem* menuItem2 = new MenuItem();
 
    menuItem1->Text = S"File";
    menuItem2->Text = S"Edit";
    // Add two MenuItem objects to the MainMenu.
    mainMenu1->MenuItems->Add(menuItem1);
    mainMenu1->MenuItems->Add(menuItem2);
    
    // Bind the MainMenu to Form1.
    Menu = mainMenu1;   
 }
 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

See Also

MainMenu Members | System.Windows.Forms Namespace | MenuItem | MenuItemCollection.Add | Menu | Menu.MenuItems

Show:
© 2014 Microsoft