Working With Menus In C#

Introduction

An imperative part of the user interface in a Windows-based application is the menu.

In this article, I elucidate how to adding menus and menuitem to Windows forms, Replacing, Cloning, Merging of menus and about Context menus (Popupmenus).

Note
In .NET 4.0 or later versions, MenuStrip control has replaced MainMenu. Please visit MenuStrip in C#.

A menu on a form is created with a MainMenu object, which is a collection of MenuItem objects. You can add menus to Windows Forms at design time by adding the MainMenu control and then adding menu items to it using the Menu Designer. Menus can also be added programmatically by adding one or more MainMenu controls to a form and adding MenuItem objects to the collection.

Now we perceive the following information regarding menus with instances.

  1. Adding Menu,Menu Items to a Menu.
  2. Adding Menu Enhancements to Windows Forms Menus.
  3. Replacing, Cloning, Merging of Menus.
  4. Context menus (Popupmenus).

Adding Menu, Menu Items to a Menu

First add a MainMenu control to the form. Then to add menu items to it add MenuItem objects to the collection. By default, a MainMenu object contains no menu items, so that the first menu item added becomes the menu heading. Menu items can also be dynamically added when they are created, such that properties are set at the time of their creation and addition.

The following program shows how to create a simple menu

The Class MenuTest1 creates a simple menu on a form. The form has a top-level File menu with menu items New,Open and Exit .The menu also includes a About menu.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11.   
  12.   
  13. namespace MenuTest  
  14. {  
  15.     public partial class MenuTest1 : Form  
  16.     {  
  17.         private MainMenu mainMenu;    
  18.   
  19.         public MenuTest1()  
  20.         {  
  21.             InitializeComponent();  
  22.             mainMenu = new MainMenu();  
  23.             MenuItem File = mainMenu.MenuItems.Add("&File");  
  24.             File.MenuItems.Add(new MenuItem("&New"));  
  25.             File.MenuItems.Add(new MenuItem("&Open"));  
  26.             File.MenuItems.Add(new MenuItem("&Exit"));  
  27.             this.Menu = mainMenu;  
  28.             MenuItem About = mainMenu.MenuItems.Add("&About");  
  29.             About.MenuItems.Add(new MenuItem("&About"));  
  30.             this.Menu = mainMenu;  
  31.             mainMenu.GetForm().BackColor = Color.Indigo;    
  32.         }  
  33.     }  
  34. }  
The Output

C# Menu

Now in this point let us see some of the Members, properties of the Menu type.

Some Members
  1. CloneMenu () -- Sets this menu to be a similar copy of another menu.
  2. MergeMenu () -- Merges an additional menus items with this one.

There are two properties named mergeType and mergeOrder

  1. mergeType -- Determine how individual menu items are handled during a menu merge and the relative location of each Menu Item in the newly merged menu.
  2. mergeOrder -- Determine the menu items' presence and location within a menu merge.
  3. GetMainMenu () -- Returns the Main Menu item that holds this menu.
  4. MdiListItem -- This property returns the MenuItem that contains the list of MDI child windows.

Some properties

  1. Index -- Gets or sets the menu items position in its parent menu.
  2. Checked -- Gets or sets a value representing whether a check mark appears nearby the text of the menu item.
  3. Text -- Gets or sets the text of the menu item.
  4. Shortcut -- Gets or sets the shortcut key connected with the menu item.
  5. Enabled -- Gets or sets a value representing whether the menu item is enabled.

To get information about other members and properties open the System.WinForms.dll with the help of ILDASM.exe as shown in the figures below.

ildasm 
 
 

Adding Menu Enhancements to Windows Forms Menus

There are four enhancements that can be added to menus to convey information to users.

  1. Check marks can be used to designate whether a feature is turned on or off.
  2. Shortcut keys are keyboard commands to access menu items within an application.
  3. To add an access key to a menu item.
  4. Separator bars are used to group related commands within a menu and make menus easier to read.

Now let us observe those things in details. I enlighten the usage of check marks in specify in Context menus. Shortcut keys are widely used to access menu items.

For Example

About.MenuItems.Add(new MenuItem("&About",new EventHandler this.About_clicked),Shortcut.F1));

So by pressing the function key F1 we can access the menu Item function.

To add an access key to a menu item just simply put ampersand (&) prior to the letter you want to be underlined as the access key. Make the text property of menu item a dash (-) to make that menu item a separator bar. In the following program I also included the Event handlers to all the menu items.

The following program shows how to create a menu with the enhancements,

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace MenuTest  
  12. {  
  13.     public partial class MenuTest : Form  
  14.     {  
  15.         private MainMenu mainMenu;  
  16.         private ContextMenu popUpMenu;  
  17.   
  18.         public MenuTest()  
  19.         {  
  20.             mainMenu = new MainMenu();  
  21.             popUpMenu = new ContextMenu();  
  22.             popUpMenu.MenuItems.Add("Hello"new EventHandler(pop_Clicked));  
  23.             this.ContextMenu = popUpMenu;  
  24.             MenuItem File = mainMenu.MenuItems.Add("&File");  
  25.             File.MenuItems.Add(new MenuItem("&New"new EventHandler(this.FileNew_clicked), Shortcut.CtrlN));  
  26.             File.MenuItems.Add(new MenuItem("&Open"new EventHandler(this.FileOpen_clicked), Shortcut.CtrlO));  
  27.             File.MenuItems.Add(new MenuItem("-"));  
  28.             File.MenuItems.Add(new MenuItem("&Exit"new EventHandler(this.FileExit_clicked), Shortcut.CtrlX));  
  29.             this.Menu = mainMenu;  
  30.             MenuItem About = mainMenu.MenuItems.Add("&About");  
  31.             About.MenuItems.Add(new MenuItem("&About"new EventHandler(this.About_clicked), Shortcut.F1));  
  32.             this.Menu = mainMenu;  
  33.             mainMenu.GetForm().BackColor = Color.Indigo;  
  34.         }  
  35.         private void FileExit_clicked(object sender, EventArgs e)  
  36.         {  
  37.             this.Close();  
  38.         }  
  39.         private void FileNew_clicked(object sender, EventArgs e)  
  40.         {  
  41.             MessageBox.Show("New""MENU_CREATION", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  42.         }  
  43.         private void FileOpen_clicked(object sender, EventArgs e)  
  44.         {  
  45.             MessageBox.Show("Open""MENU_CREATION", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  46.         }  
  47.         private void pop_Clicked(object sender, EventArgs e)  
  48.         {  
  49.             MessageBox.Show("Popupmenu""MENU_CREATION", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  50.         }  
  51.         private void About_clicked(object sender, EventArgs e)  
  52.         {  
  53.             MessageBox.Show("G.GNANA ARUN GANESH""[email protected]");  
  54.         }  
  55.     }  
  56. }  
 The output

c# menu
C# Menu

Replacing, Cloning, Merging of Menus

At times, when creating Windows Forms applications, we require generating a copy of a menu that already created. In such circumstances, rather than recreating complete menu structures when you want to duplicate a given menu's functionality, we can use the CloneMenu method.

Likewise it is useful to have the items from two menus appear in a single menu, such as merging a Multiple Document Interface (MDI) container's menu with that of its active MDI child. Similarly in some situations depending on the run-time requirements of the application we have to Add, Delete the menus or Enabling, disabling of the menu items.

Now let us see all those things in details with an example.

In the following program it has a top-level File menu with menu items Merge (Edit-View), Remove (Merged), Disable (View), Enable (View), Replace (Edit-View). The menu also includes a View menu and Edit menu. If we click the Merge (Edit-View) menu item then it creates a new top-level menu named "Merged" by merging the edit menu and view menu.

If we click the Remove (Merged) menu item then it removes the newly created "Merged" menu. If we click the Disable (View) menu item then it disables the menu items of View menu. It is useful to limit or broaden the commands a user may make. If we click the Enable (View) menu item then it enables the menu items of View menu. If we click the Replace (Edit-View) menu item then it replaces the position of the Edit and View menus.

The Example

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace MenuTest  
  12. {  
  13.     public partial class MenuTest2 : Form  
  14.     {  
  15.         private MainMenu mainMenu;  
  16.         MenuItem Editmen, Viewmen;  
  17.   
  18.         public MenuTest2()  
  19.         {  
  20.             mainMenu = new MainMenu();  
  21.             MenuItem File = mainMenu.MenuItems.Add("&File");  
  22.             File.MenuItems.Add(new MenuItem("&Merge(Edit-View)"new EventHandler(this.Merge_clicked), Shortcut.CtrlM));  
  23.             File.MenuItems.Add(new MenuItem("&Remove(Merged)"new EventHandler(this.Remove_clicked), Shortcut.CtrlR));  
  24.             File.MenuItems.Add(new MenuItem("&Disable(View)"new EventHandler(this.Disable_clicked), Shortcut.CtrlD));  
  25.             File.MenuItems.Add(new MenuItem("&Enable(View)"new EventHandler(this.Enable_clicked), Shortcut.CtrlE));  
  26.             File.MenuItems.Add(new MenuItem("&Replace(Edit-View)"new EventHandler(this.Replace_clicked), Shortcut.CtrlP));  
  27.             File.MenuItems.Add(new MenuItem("-"));  
  28.             File.MenuItems.Add(new MenuItem("&Exit"new EventHandler(this.FileExit_clicked), Shortcut.CtrlX));  
  29.             Viewmen = new MenuItem("&View");  
  30.             Viewmen.MenuItems.Add(new MenuItem("List"));  
  31.             Viewmen.MenuItems.Add(new MenuItem("Thumbnails"));  
  32.             Viewmen.MenuItems.Add(new MenuItem("Details"));  
  33.             mainMenu.MenuItems.Add(Viewmen.CloneMenu());  
  34.             this.Menu = mainMenu;  
  35.             Editmen = new MenuItem("&Edit");  
  36.             Editmen.MenuItems.Add(new MenuItem("Cut"));  
  37.             Editmen.MenuItems.Add(new MenuItem("Copy"));  
  38.             Editmen.MenuItems.Add(new MenuItem("Paste"));  
  39.             mainMenu.MenuItems.Add(Editmen.CloneMenu());  
  40.             this.Menu = mainMenu;  
  41.             mainMenu.GetForm().BackColor = Color.Indigo;  
  42.         }  
  43.   
  44.   
  45.         private void FileExit_clicked(object sender, EventArgs e)  
  46.         {  
  47.             this.Close();  
  48.         }  
  49.         private void Merge_clicked(object sender, EventArgs e)  
  50.         {  
  51.             Viewmen.MergeMenu(Editmen.CloneMenu());  
  52.             mainMenu.MenuItems.Add(Viewmen);  
  53.             Viewmen.Text = "&Merged";  
  54.             this.Menu = mainMenu;  
  55.         }  
  56.         private void Remove_clicked(object sender, EventArgs e)  
  57.         {  
  58.             mainMenu.MenuItems.Remove(Viewmen);  
  59.         }  
  60.         private void Enable_clicked(object sender, EventArgs e)  
  61.         {  
  62.             mainMenu.MenuItems[1].MenuItems[0].Visible = true;  
  63.             mainMenu.MenuItems[1].MenuItems[1].Visible = true;  
  64.             mainMenu.MenuItems[1].MenuItems[2].Visible = true;  
  65.         }  
  66.         private void Disable_clicked(object sender, EventArgs e)  
  67.         {  
  68.             mainMenu.MenuItems[1].MenuItems[0].Visible = false;  
  69.             mainMenu.MenuItems[1].MenuItems[1].Visible = false;  
  70.             mainMenu.MenuItems[1].MenuItems[2].Visible = false;  
  71.         }  
  72.         private void Replace_clicked(object sender, EventArgs e)  
  73.         {  
  74.             mainMenu.MenuItems[1].Index++;  
  75.         }  
  76.     }  
  77. }  
The output
 
 c# menu c# menu
 
Context menus (Popup menus)

Context menus are used inside applications to provide users access to often used commands by means of a right-click of the mouse. Often, context menus are assigned to controls, and provide particular commands that relate to that precise control.

The Example
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace MenuTest  
  12. {  
  13.     public partial class MenuTest3 : Form  
  14.     {  
  15.         private ContextMenu popUpMenu;  
  16.         private MenuItem Currentcheck;  
  17.         private MenuItem checkBold;  
  18.         private MenuItem checkItalic;  
  19.         private MenuItem checkUnderline;  
  20.   
  21.         public MenuTest3()  
  22.         {  
  23.             InitializeComponent();  
  24.   
  25.             popUpMenu = new ContextMenu();  
  26.             popUpMenu.MenuItems.Add("Bold"new EventHandler(popup));  
  27.             popUpMenu.MenuItems.Add("Italic"new EventHandler(popup));  
  28.             popUpMenu.MenuItems.Add("Underline"new EventHandler(popup));  
  29.             this.ContextMenu = popUpMenu;  
  30.             this.BackColor = Color.Sienna;  
  31.             checkBold = this.ContextMenu.MenuItems[0];  
  32.             checkItalic = this.ContextMenu.MenuItems[1];  
  33.             checkUnderline = this.ContextMenu.MenuItems[2];  
  34.             Currentcheck = checkBold;  
  35.             Currentcheck.Checked = true;  
  36.         }  
  37.         private void popup(object sender, EventArgs e)  
  38.         {  
  39.             Currentcheck.Checked = false;  
  40.             MenuItem miClicked = (MenuItem)sender;  
  41.             string item = miClicked.Text;  
  42.             if (item == "Bold")  
  43.             {  
  44.                 Currentcheck = checkBold;  
  45.                 MessageBox.Show("Bold");  
  46.             }  
  47.             if (item == "Underline")  
  48.             {  
  49.                 Currentcheck = checkUnderline;  
  50.                 MessageBox.Show("Undreline");  
  51.             }  
  52.             if (item == "Italic")  
  53.             {  
  54.                 Currentcheck = checkItalic;  
  55.                 MessageBox.Show("Italic");  
  56.             }  
  57.             Currentcheck.Checked = true;  
  58.             Invalidate();  
  59.         }  
  60.     }  
  61. }  
The output

c# menu
 
Note
In .NET 4.0 or later versions, MenuStrip control has replaced MainMenu. Please visit MenuStrip in C#.

Working with Menus in Visual Studio 2010

If you are using Visual Studio 2010 and .NET 4.0, the menus functionality is replaced with the MenuStrip control in .NET 4.0.

Here is a detailed tutorial and some more related articles,


Similar Articles