1 out of 5 rated this helpful - Rate this topic

MainMenu Class

Represents the menu structure of a form. Although MenuStrip replaces and adds functionality to the MainMenu control of previous versions, MainMenu is retained for both backward compatibility and future use if you choose.

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

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public class MainMenu : Menu

The MainMenu type exposes the following members.

  Name Description
Public method MainMenu() Initializes a new instance of the MainMenu class without any specified menu items.
Public method MainMenu(IContainer) Initializes a new instance of the MainMenu class with the specified container.
Public method MainMenu(MenuItem[]) Initializes a new instance of the MainMenu with a specified set of MenuItem objects.
Top
  Name Description
Protected property CanRaiseEvents Gets a value indicating whether the component can raise an event. (Inherited from Component.)
Public property Container Gets the IContainer that contains the Component. (Inherited from Component.)
Protected property DesignMode Gets a value that indicates whether the Component is currently in design mode. (Inherited from Component.)
Protected property Events Gets the list of event handlers that are attached to this Component. (Inherited from Component.)
Public property Handle Gets a value representing the window handle for the menu. (Inherited from Menu.)
Public property IsParent Gets a value indicating whether this menu contains any menu items. This property is read-only. (Inherited from Menu.)
Public property MdiListItem Gets a value indicating the MenuItem that is used to display a list of multiple document interface (MDI) child forms. (Inherited from Menu.)
Public property MenuItems Gets a value indicating the collection of MenuItem objects associated with the menu. (Inherited from Menu.)
Public property Name Gets or sets the name of the Menu. (Inherited from Menu.)
Public property RightToLeft Gets or sets whether the text displayed by the control is displayed from right to left.
Public property Site Gets or sets the ISite of the Component. (Inherited from Component.)
Public property Tag Gets or sets user-defined data associated with the control. (Inherited from Menu.)
Top
  Name Description
Public method CloneMenu() Creates a new MainMenu that is a duplicate of the current MainMenu.
Protected method CloneMenu(Menu) Copies the Menu that is passed as a parameter to the current Menu. (Inherited from Menu.)
Protected method CreateMenuHandle Creates a new handle to the Menu. (Overrides Menu.CreateMenuHandle().)
Public method CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public method Dispose() Releases all resources used by the Component. (Inherited from Component.)
Protected method Dispose(Boolean) Disposes of the resources, other than memory, used by the MainMenu. (Overrides Menu.Dispose(Boolean).)
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection. (Inherited from Component.)
Public method FindMenuItem Gets the MenuItem that contains the value specified. (Inherited from Menu.)
Protected method FindMergePosition Returns the position at which a menu item should be inserted into the menu. (Inherited from Menu.)
Public method GetContextMenu Gets the ContextMenu that contains this menu. (Inherited from Menu.)
Public method GetForm Gets the Form that contains this control.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public method GetMainMenu Gets the MainMenu that contains this menu. (Inherited from Menu.)
Protected method GetService Returns an object that represents a service provided by the Component or by its Container. (Inherited from Component.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method InitializeLifetimeService Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected method MemberwiseClone() Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method MemberwiseClone(Boolean) Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Public method MergeMenu Merges the MenuItem objects of one menu with the current menu. (Inherited from Menu.)
Protected method OnCollapse Raises the Collapse event.
Protected method ProcessCmdKey Processes a command key. (Inherited from Menu.)
Public method ToString Infrastructure. Returns a string that represents the MainMenu. (Overrides Menu.ToString().)
Top
  Name Description
Public event Collapse Occurs when the main menu collapses.
Public event Disposed Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)
Top

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.

You can create different MainMenu objects to represent different menu structures 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.

Note Note

Cutting and pasting menu items from one form to another in the designer might not work as expected if the form you are pasting into has no menu items defined.

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.


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;   
}



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ