Home

Applications Menus: The Main Menu

   

The Main Menu

 

Introduction

When it comes to a restaurant, a menu is a list of food items that the business offers to its customers. For a computer application, a menu is a list of actions that can be performed on that program. To be aware of these actions, the list must be presented to the user upon request. On a typical DOS application, a menu is presented with numerical or character options that the user can select from. An example would be:

Here are the various options:
1. Register a new student
2. Review a student's information
3. Enter student's grades
4. Close the application

The user would then enter the number (or character) that corresponds to the desired option and continue using the program. For a graphical application, a menu is presented as a list of words and, using a mouse or a keyboard, the user can select the desired item from the menu.

To enhance the functionality of a graphical application, also to take advantage of the mouse and the keyboard, there are various types of menus. A menu is considered a main menu when it carries most of the actions the user can perform on a particular application. Such a menu is positioned in the top section of the form in which it is used.

A main menu is divided in categories of items and each category is represented by a word. In WordPad, the categories of menus are File, Edit, View, Insert, Format, and Help:

Main Menu

To use a menu, the user first clicks one of the words that displays on top. When clicked, the menu expands and displays a list of items that belong to that category. Here is an example:

Menu Category

There is no strict rule on how a menu is organized. There are only suggestions. For example, actions that are related to file processing, such as creating a new file, opening an existing file, saving a file, printing the open file, or closing the file usually stay under a category called File. In the same way, actions related to viewing documents can be listed under a View menu category.

Main Menu Creation

To support actions that are used to graphically enhance the functionality of an application, the .NET Framework provides the ToolStrip class. To support menus for an application, the .NET Framework provides the MenuStrip class (in Microsoft Visual Studio 2002 and 2003, the main menu was implemented through the MainMenu class, which is still available but lacks some features).

To graphically create a main menu, in the Menus & Toolbars section of the Toolbox, you can click the MenuStrip button MenuStrip and click the form that will use the menu. After clicking the form, an empty menu is initiated:

Main Menu

Like every control, the main menu must have a name. After adding the menu strip to a form, you can accept the suggested name or, in the Properties window, click (Name) and type the desired name. You can then use the menu placeholder to add the necessary menu item(s)..

To programmatically create a main menu, declare a handle to MenuStrip class and initialize it with its default constructor. Because the main menu is primarily a control, you must add it to the list of controls of the form. Here is an example:

using System;
using System.Windows.Forms;

public class Exercise : Form
{
    MenuStrip mnuMain;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        mnuMain = new MenuStrip();
        Controls.Add(mnuMain);
    }

    static int Main()
    {
        System.Windows.Forms.Application.Run(new Exercise());
        return 0;
    }
}

Menu Categories

In our introduction, we saw that a main menu was made of categories represented in the top section. After adding a MenuStrip, you can start creating the desired menu categories. To graphically create a menu category:

  • In the menu strip, you can click the Type Here line and type the desired string
  • In the menu strip, you can click Type Here. Then, in the Properties window, click the Text field and type the desired string

To create the next menu category, you can click Type Here on the right side of the previous menu category. In the same way, you can continue creating the desired menu categories.

Here is an example:

Main Menu

Besides clicking Type Here and typing a string, an alternative is to get assisted by a dialog box. To open it:

  • Under the form, you can click the menu strip object and, in the Properties window, you can click the ellipsis button of the Items field
  • Under the form, you can right-click the menu strip and click Edit Items...

A dialog box, titled Items Collection Editor, would come up:

Items Collection Editor

To create a menu category, in the Select Item And Add To List Below combo box, select MenuItem and click the Add button. In the right list, configure the menu item. At a minimum, you should specify its caption in the Text field. Like every control, each menu item has a name. To make sure you can easily recognize it in your code, when creating a menu item, you should give it a name unless you are contempt with the suggested one. After creating the menu categories, you can click OK and keep the dialog box opened for more options.

To support menu items, the .NET Framework provides the ToolStripMenuItem class. Using it, to create a menu category, declare a handle to this class and initialize it using one of its constructors. The default constructor is used to create a menu item without specifying any of its options. Here is an example:

ppublic class Exercise : System.Windows.Forms.Form
{
    MenuStrip mnuMain;
    ToolStripMenuItem mnuFile;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        mnuMain = new MenuStrip();
        mnuFile = new ToolStripMenuItem();

        Controls.Add(mnuMain);
    }
}

To specify the caption that will be displayed on a menu category, you can use the following constructor of the ToolStripMenuItem class:

public ToolStripMenuItem(string text);

Here is an example:

public class Exercise : System.Windows.Forms.Form
{
    MenuStrip mnuMain;
    ToolStripMenuItem mnuFile;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        mnuMain = new MenuStrip();
        mnuFile = new ToolStripMenuItem("File");

        Controls.Add(mnuMain);
    }
}

If you had instantiated the class using its default constructor, to specify its caption, the ToolStripMenuItem class is equipped with the Text property. Therefore, assign a string to this property. Here is an example:

public class Exercise : System.Windows.Forms.Form
{
    MenuStrip mnuMain;
    ToolStripMenuItem mnuFile;
    ToolStripMenuItem mnuEdit;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        mnuMain = new MenuStrip();
        mnuFile = new ToolStripMenuItem("File");
        mnuEdit = new ToolStripMenuItem();
        mnuEdit.Text = "Edit";

        Controls.Add(mnuMain);
    }
}

In the same way, you can create as many menu categories as you judge necessary for your application.

Introduction to Menu Items

In our introduction, we saw that if you click a menu category, a list comes up. Here is an example:

Menu expanded

The objects under a menu category are referred to as menu items. To graphically create a menu item, first access the menu strip, which you can do by clicking it under the form. On the form, click a menu category. Once you do, a placeholder would be displayed under it:

To create a menu item:

  • Under a category, click Type Here and type the desired caption. 
  • Click the menu category. Then, in the Properties window, click Text and type the desired string.

In the same way, to create the next menu item, under the same category, click the next Type Here and type the desired caption or change the Text value in the Properties window.

An alternative is to use a dialog box. To access it, in the menu designer:

  • Right-click the menu category and click Edit Drop Down Items...
  • Click the menu category. Then, in the Properties window, click the ellipsis button of the DropDownItems field

The Items Collection Editor dialog box would come up:

Items Collection Editor

To create a menu item, in the Select Item And Add To List Below combo box, select MenuItem and click Add. On the right side, configure the menu item as you see fit. At a minimum, you should specify its caption in the Text field.

Both the menu category and the menu item are created using the ToolStripMenuItem class. Here are examples:

public class Exercise : System.Windows.Forms.Form
{
    MenuStrip mnuMain;
    
    ToolStripMenuItem mnuFile;
    ToolStripMenuItem mnuNew;
    ToolStripMenuItem mnuExit;
    ToolStripMenuItem mnuEdit;
    ToolStripMenuItem mnuCopy;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        mnuMain = new MenuStrip();
        
        mnuFile = new ToolStripMenuItem("File");
        mnuNew  = new ToolStripMenuItem("New");
        mnuExit = new ToolStripMenuItem("Exit");
        mnuEdit = new ToolStripMenuItem("Edit");
        mnuCopy = new ToolStripMenuItem("Copy");

        Controls.Add(mnuMain);
    }
}

Associating Menu Items to Menu Categories

If you visually create your main menu, the form designer takes care of most details behind the scenes. For example, each menu item is automatically added to its parent menu category. If you programmatically create your main menu, you must associate each menu item to its parent menu category.

To support menu categories, ToolStripMenuItem, the class used to create menu categories, is derived from a class named ToolStripDropDownItem. The ToolStripDropDownItem class is abstract, which means you cannot instantiate it. Instead, it provides functionality to other classes derived from it. The ToolStripDropDownItem class is based on the ToolStripItem class.

To support menu items, the ToolStripDropDownItem class is equipped with a property named DropDownItems. This property is of type ToolStripItemCollection, which a collection-based class. The ToolStripItemCollection class implements the IList and the ICollection interfaces.

To specify that a menu item will be part of a menu category, call the Add() method of the ToolStripItemCollection class. This method is overloaded with various versions. One of the versions uses the following syntax:

public int Add(ToolStripItem value);

This version allows you to pass a ToolStripItem-type of item class, such as a ToolStripMenuItem object. Here is an example:

public class Exercise : System.Windows.Forms.Form
{
    MenuStrip mnuMain;
    
    ToolStripMenuItem mnuEdit;
    ToolStripMenuItem mnuCopy;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        mnuMain = new MenuStrip();
        
        mnuEdit = new ToolStripMenuItem("Edit");
        mnuCopy = new ToolStripMenuItem("Copy");
        mnuEdit.DropDownItems.Add(mnuCopy);

        Controls.Add(mnuMain);
    }
}

The ToolStripItemCollection class also allows you to create a menu item without going through a ToolStripItem-type of object. To support this, its provides the following version of its Add() method:

public ToolStripItem Add(string text);

This method takes as argument the text that the menu item would display and it returns the ToolStripItem item that was created. Here is an example:

public class Exercise : System.Windows.Forms.Form
{
    MenuStrip mnuMain;
    
    ToolStripMenuItem mnuEdit;
    ToolStripMenuItem mnuCopy;
    ToolStripMenuItem mnuPaste;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        mnuMain = new MenuStrip();
        
        mnuEdit = new ToolStripMenuItem("Edit");
        mnuCopy	= new ToolStripMenuItem("Copy");
        mnuEdit.DropDownItems.Add(mnuCopy);
        mnuPaste = (ToolStripMenuItem)mnuEdit.DropDownItems.Add("Paste");

        Controls.Add(mnuMain);
    }
}

Instead of adding one menu item at a time, you can create an array of menu items and then add it to a category in one row. To support this, the ToolStripItemCollection class implements the AddRange() method. This method is overloaded with two versions. One of the versions uses the following syntax:

public void AddRange(ToolStripItem[] toolStripItems);

When calling this method, you must pass it an array of ToolStripItem-type of objects. Here are two examples:

public class Exercise : System.Windows.Forms.Form
{
    MenuStrip mnuMain;
    
    ToolStripMenuItem mnuFile;
    ToolStripMenuItem mnuNew;
    ToolStripMenuItem mnuOpen;
    ToolStripMenuItem mnuExit;
    ToolStripMenuItem mnuEdit;
    ToolStripMenuItem mnuCopy;
    ToolStripMenuItem mnuPaste;
    ToolStripMenuItem mnuHelp;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        mnuMain = new MenuStrip();

        mnuFile = new ToolStripMenuItem("File");
        mnuNew = new ToolStripMenuItem("New");
        mnuOpen = new ToolStripMenuItem("Open");
        mnuExit = new ToolStripMenuItem("Exit");
        ToolStripMenuItem[] mnuFileItems = { mnuNew, mnuOpen, mnuExit };
        mnuFile.DropDownItems.AddRange(mnuFileItems);

        mnuEdit = new ToolStripMenuItem("Edit");
        mnuCopy = new ToolStripMenuItem("Copy");
        mnuEdit.DropDownItems.Add(mnuCopy);
        mnuPaste = (ToolStripMenuItem)mnuEdit.DropDownItems.Add("Paste");

        mnuHelp = new ToolStripMenuItem("Help");
        ToolStripMenuItem[] mnuHelpItems =
        { 
            new ToolStripMenuItem("Search"),
            new ToolStripMenuItem("Contents"),
            new ToolStripMenuItem("Index"),
            new ToolStripMenuItem("Support Web Site"),
            new ToolStripMenuItem("About this application")
        };
        mnuHelp.DropDownItems.AddRange(mnuHelpItems);

        Controls.Add(mnuMain);
    }
}

Associating Menu Categories to the Main Menu 

If you visually create your main menu, each menu category is automatically assigned to the menu strip. If you programmatically create your main menu, you must take care of this in order to show the whole menu.

After creating the menu categories, you can add them to the main menu. To support this, the ToolStrip class is equipped with a property named Items and it makes this property available to the MenuStrip class. The Items property is of type ToolStripItemCollection. This class implements the IList, the ICollection, and the IEnumerable interfaces. Therefore, to add a menu category to a MenuStrip object, you can call the Add() method of the ToolStripItemCollection class. This method is overloaded with various versions and one of them uses the following version:

public int Add(ToolStripItem value);

You can call this version and pass it a ToolStripItem-type of object, such as a ToolStripMenuItem value. Here is an example:

public class Exercise : System.Windows.Forms.Form
{
    MenuStrip mnuMain;
    
    ToolStripMenuItem mnuFile;
    ToolStripMenuItem mnuNew;
    ToolStripMenuItem mnuOpen;
    ToolStripMenuItem mnuExit;
    ToolStripMenuItem mnuEdit;
    ToolStripMenuItem mnuCopy;
    ToolStripMenuItem mnuPaste;
    ToolStripMenuItem mnuHelp;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        mnuMain = new MenuStrip();

        mnuFile = new ToolStripMenuItem("File");
        mnuNew = new ToolStripMenuItem("New");
        mnuOpen = new ToolStripMenuItem("Open");
        mnuExit = new ToolStripMenuItem("Exit");
        ToolStripMenuItem[] mnuFileItems = { mnuNew, mnuOpen, mnuExit };
        mnuFile.DropDownItems.AddRange(mnuFileItems);

        mnuEdit = new ToolStripMenuItem("Edit");
        mnuCopy = new ToolStripMenuItem("Copy");
        mnuEdit.DropDownItems.Add(mnuCopy);
        mnuPaste = (ToolStripMenuItem)mnuEdit.DropDownItems.Add("Paste");

        mnuHelp = new ToolStripMenuItem("Help");
        ToolStripMenuItem[] mnuHelpItems =
        { 
            new ToolStripMenuItem("Search"),
            new ToolStripMenuItem("Contents"),
            new ToolStripMenuItem("Index"),
            new ToolStripMenuItem("Support Web Site"),
            new ToolStripMenuItem("About this application")
        };
        mnuHelp.DropDownItems.AddRange(mnuHelpItems);

        mnuMain.Items.Add(mnuFile);
        Controls.Add(mnuMain);
    }
}

In the same way, you can add the other items. Alternatively, you can create an array of menu categories and add them in a row. To support this, the ToolStripItemCollection is equipped with the AddRange() method that is overloaded with two versions. One of the versions uses the following syntax:

public void AddRange(ToolStripItem[] toolStripItems);

When calling this method, pass it an array of ToolStripItem types of values. Here is an example:

public class Exercise : System.Windows.Forms.Form
{
    MenuStrip mnuMain;
    
    ToolStripMenuItem mnuFile;
    ToolStripMenuItem mnuNew;
    ToolStripMenuItem mnuOpen;
    ToolStripMenuItem mnuExit;
    ToolStripMenuItem mnuEdit;
    ToolStripMenuItem mnuCopy;
    ToolStripMenuItem mnuPaste;

    ToolStripMenuItem mnuView;

    ToolStripMenuItem mnuHelp;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        mnuMain = new MenuStrip();

        mnuFile = new ToolStripMenuItem("File");
        mnuNew = new ToolStripMenuItem("New");
        mnuOpen = new ToolStripMenuItem("Open");
        mnuExit = new ToolStripMenuItem("Exit");
        ToolStripMenuItem[] mnuFileItems = { mnuNew, mnuOpen, mnuExit };
        mnuFile.DropDownItems.AddRange(mnuFileItems);

        mnuEdit = new ToolStripMenuItem("Edit");
        mnuCopy = new ToolStripMenuItem("Copy");
        mnuEdit.DropDownItems.Add(mnuCopy);
        mnuPaste = (ToolStripMenuItem)mnuEdit.DropDownItems.Add("Paste");

        mnuView = new ToolStripMenuItem("View");
        ToolStripMenuItem[] mnuViewItems =
        {
	    new ToolStripMenuItem("Standard Toolbar"),
	    new ToolStripMenuItem("Formatting Toolbar"),
	    new ToolStripMenuItem("Status Bar")
        };
        mnuView.DropDownItems.AddRange(mnuViewItems);

        mnuHelp = new ToolStripMenuItem("Help");
        ToolStripMenuItem[] mnuHelpItems =
        { 
            new ToolStripMenuItem("Search"),
            new ToolStripMenuItem("Contents"),
            new ToolStripMenuItem("Index"),
            new ToolStripMenuItem("Support Web Site"),
            new ToolStripMenuItem("About this application")
        };
        mnuHelp.DropDownItems.AddRange(mnuHelpItems);

        ToolStripMenuItem[] mnuAccessories = { mnuView, mnuHelp };

        mnuMain.Items.Add(mnuFile);
        mnuMain.Items.AddRange(mnuAccessories);

        Controls.Add(mnuMain);
    }
}

This would produce:

Main menu

ApplicationApplication: Creating a Menu

  1. Start Microsoft Visual Studio
  2. Create a new Windows Forms Application named AltairRealtors1
  3. From the Menus & Toolbars section of the Toolbox, click the MenuStrip button MenuStrip and click the form
  4. While the menu strip is still selected, in the Properties window, click (Name), type mnuMain and press Enter
  5. On the form, click Type Here, type File and press Enter
  6. On the form, click File.
    In the Properties window, click (Name) and type mnuFile
  7. On the form, click File and under it, click the Type Here box
  8. Type New Property and press Enter
  9. On the form, click File and click New Property.
    In the Properties window, click (Name) and type mnuFileNewProperty
  10. On the form, click File and, under New Property, click the Type Here box
  11. Type Exit and press Enter
  12. On the form, click File and click Exit.
    In the Properties window, click (Name) and type mnuFileExit
     
    Altair Realtors - Available Properties Listing
  13. On the main menu, click Project -> Add Windows Form...
  14. Set the Name to RealEstateProperty and click Add
  15. To display the first form, on the main menu, click Windows -> Form1.cs [Design]*

Coding a Menu Item

If you create a menu as we have just done, to write code for one of the menu items, you can double-click the menu item. This would open the Click event of the menu item in the Code Editor and you can start writing the desired code for that item.

ApplicationApplication: Writing Code For a Main Menu

  1. On the form, click File and double-click New Property
  2. Implement the event as follows:
    using System;
    
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace AltairRealtors1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void mnuFileNewProperty_Click(object sender, EventArgs e)
            {
                RealEstateProperty dlgProperty = new RealEstateProperty();
    
                dlgProperty.ShowDialog();
            }
        }
    }
  3. Return to the (first) form
  4. On the form, click File and double-click Exit
  5. Implement the event as follows:
    private void mnuFileExit_Click(object sender, EventArgs e)
    {
        Close();
    }
  6. To build and execute, on the main menu, click Debug -> Start Debugging
     
    Using a menu item at execution time
  7. To close it, click File -> Exit

Application Application: Introducing Contextual Menus

  1. From the Menus & Toolbars section of the Toolbox, click ContextMenuStrip and click the form
  2. While the context menu strip is still selected, in the Properties window click (Name) and type mnuWithProperties
  3. Then click Items and click its ellipsis button
  4. Under Select Item And Add To List Below, make sure MenuItem is selected and click Add
  5. On the right side, click Text and type Edit
  6. Click (Name) and type mnuEditProperty
  7. On the left side, click Add and, on the right side, change the properties as follows:
    Text: Delete
    (Name): mnuDeleteProperty
  8. On the left side, click Add and, on the right side, change the properties as follows:
    Text: Clear
    (Name): mnuClearProperties
     
    Items Collection Editor
  9. Click OK
  10. From the Menus & Toolbars section of the Toolbox, click ContextMenuStrip and click the form
  11. While the context menu strip is still selected, in the Properties window click (Name) and type mnuNoProperty
  12. On the form, under ContextMenuStrip, click Type Here
  13. Type New Property and press Enter
  14. On the form, under ContextMenuStrip, click New Property
  15. In the Properties window, click (Name), type mnuNewProperty and press Enter

Using a Contextual Menu

By default, a

After assigning a ContextMenuStrip object to a control, when you right-click (actually when the user right-clicks) the control, the contextual menu would display. The above code would produce:

ApplicationApplication: Closing

  1. Close your programming environment
  2. When asked whether you want to save, click No
 

Home Copyright © 2010-2016, FunctionX