MainMenu Class

Definition

Represents the menu structure of a form.

This class is not available in .NET Core 3.1 and later versions. Use MenuStrip instead, which replaces and extends the MainMenu control.

public ref class MainMenu : System::Windows::Forms::Menu
public class MainMenu : System.Windows.Forms.Menu
type MainMenu = class
    inherit Menu
Public Class MainMenu
Inherits Menu
Inheritance

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 class is not available in .NET Core 3.1 and later versions. Use MenuStrip instead.

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

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.

Constructors

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.

Fields

FindHandle

Specifies that the FindMenuItem(Int32, IntPtr) method should search for a handle.

(Inherited from Menu)
FindShortcut

Specifies that the FindMenuItem(Int32, IntPtr) method should search for a shortcut.

(Inherited from Menu)

Properties

CanRaiseEvents

Gets a value indicating whether the component can raise an event.

(Inherited from Component)
Container

Gets the IContainer that contains the Component.

(Inherited from Component)
DesignMode

Gets a value that indicates whether the Component is currently in design mode.

(Inherited from Component)
Events

Gets the list of event handlers that are attached to this Component.

(Inherited from Component)
Handle

Gets a value representing the window handle for the menu.

(Inherited from Menu)
IsParent

Gets a value indicating whether this menu contains any menu items. This property is read-only.

(Inherited from Menu)
MdiListItem

Gets a value indicating the MenuItem that is used to display a list of multiple document interface (MDI) child forms.

(Inherited from Menu)
MenuItems

Gets a value indicating the collection of MenuItem objects associated with the menu.

(Inherited from Menu)
Name

Gets or sets the name of the Menu.

(Inherited from Menu)
RightToLeft

Gets or sets whether the text displayed by the control is displayed from right to left.

Site

Gets or sets the ISite of the Component.

(Inherited from Component)
Tag

Gets or sets user-defined data associated with the control.

(Inherited from Menu)

Methods

CloneMenu()

Creates a new MainMenu that is a duplicate of the current MainMenu.

CloneMenu(Menu)

Copies the Menu that is passed as a parameter to the current Menu.

(Inherited from Menu)
CreateMenuHandle()

Creates a new handle to the Menu.

CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Dispose()

Releases all resources used by the Component.

(Inherited from Component)
Dispose(Boolean)

Disposes of the resources, other than memory, used by the MainMenu.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
FindMenuItem(Int32, IntPtr)

Gets the MenuItem that contains the value specified.

(Inherited from Menu)
FindMergePosition(Int32)

Returns the position at which a menu item should be inserted into the menu.

(Inherited from Menu)
GetContextMenu()

Gets the ContextMenu that contains this menu.

(Inherited from Menu)
GetForm()

Gets the Form that contains this control.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()
Obsolete.

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetMainMenu()

Gets the MainMenu that contains this menu.

(Inherited from Menu)
GetService(Type)

Returns an object that represents a service provided by the Component or by its Container.

(Inherited from Component)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService()
Obsolete.

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
MergeMenu(Menu)

Merges the MenuItem objects of one menu with the current menu.

(Inherited from Menu)
OnCollapse(EventArgs)

Raises the Collapse event.

ProcessCmdKey(Message, Keys)

Processes a command key.

(Inherited from Menu)
ToString()

Returns a string that represents the MainMenu.

Events

Collapse

Occurs when the main menu collapses.

Disposed

Occurs when the component is disposed by a call to the Dispose() method.

(Inherited from Component)

Applies to

See also