Home

Windows Applications Menus:
Grouping Menu Items

 

Menu Separators

As we will see in later sections, there are various ways you can make a menu look good and you have many options to configure menu items. One of the ways you can manage menu items is to group them in small entities of your choice. You can do this either for the looks or for aesthetic reasons.

A menu separator is a horizontal line among some menu items to visually divide them. Here is an example:

There are two reasons you would use a separator. You can use a separator just for aesthetic reasons, to make your menu look good. Another, more valuable reason, is to create groups of menu items and show their belonging together by showing a line separating one group from another.

To visually specify a separator, when creating the menu item, set its string to a simple -.

To support menu separators, the .NET Framework provides the ToolStripSeparator class, which is derived from ToolStripItem. To programmatically create a separator, declare a handle to ToolStripSeparator, initialize it using the gcnew operator, add it to the Items property of the ToolStripItem menu category that will hold the separator. Here is an example:

#include <windows.h>

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;

public ref class CExercise : public Form
{
private:
    MenuStrip ^ mnuMain;
    ToolStripMenuItem  ^ mnuFile;
    ToolStripMenuItem  ^ mnuFileNew;
    ToolStripSeparator ^ mnuSeparator;
    ToolStripMenuItem  ^ mnuFileExit;
    ToolStripMenuItem  ^ mnuFormat;
    ToolStripMenuItem  ^ mnuFormatFont;

public:
    CExercise()
    {
	InitializeComponent();
    }

    void InitializeComponent()
    {
	mnuMain = gcnew MenuStrip;
	mnuFile = gcnew ToolStripMenuItem(L"&File");
	mnuFileNew = gcnew ToolStripMenuItem(L"&New");
	mnuFileNew->ShortcutKeys = Keys::Control | Keys::N;
	mnuSeparator = gcnew ToolStripSeparator;

	mnuFileExit = gcnew ToolStripMenuItem(L"E&xit");

	mnuFormat = gcnew ToolStripMenuItem(L"For&mat");
	mnuFormatFont = gcnew ToolStripMenuItem(L"Fo&nt");
	mnuFormatFont->ShortcutKeys = Keys::F4;

	mnuFile->DropDownItems->Add(mnuFileNew);
	mnuFile->DropDownItems->Add(mnuSeparator);
	mnuFile->DropDownItems->Add(mnuFileExit);
	mnuMain->Items->Add(mnuFile);

	mnuFormat->DropDownItems->Add(mnuFormatFont);
	mnuMain->Items->Add(mnuFormat);

	Controls->Add(mnuMain);
    }
};

int APIENTRY WinMain(HINSTANCE hInstance,
		     HINSTANCE hPrevInstance,
		     LPSTR lpCmdLine,
		     int nCmdShow)
{
    Application::Run(gcnew CExercise());

    return 0;
}

This would produce:

Sub-Menus

If you have menu items that perform similar tasks, you can put them in a group, which you can do using line separators. Another option is to create the menu items in their own group. The group of menu items that are created as children of a parent menu is referred to as a sub-menu. Here is an example of a sub-menu in Microsoft Paint:

To visually create a sub-menu, under the form, click the menu control that will hold the items. In the menu designer

  • If the sub-menu will be created for a main menu item, first click the menu category, then click the menu item that will hold the sub-menu
  • If the sub-menu will be created for a contextual menu, click the menu item that will hold the sub-menu

After selecting the eventual parent of the intended sub-menu, click the right Type Here box, type the desired caption and optionally give it a name.

To create another item for the sub-menu, you can click the Type Here box under the previous one. In the same way, you can add as many items as you judge necessary. Here is an example:

You can also create a sub-menu for a menu item that itself is a sub-menu. Here is an example:

To create a sub-menu for an item A that itself is a sub-menu, click that menu item A, click the Type Here box on the right side, and type its caption.

As another technique, after selecting the menu item that will act as the parent of the sub-menu, in the Properties window, click the ellipsis button of the DropDownItems field to open the Items Collection Editor dialog box. To create an item for the sub-menu, in the top combo box, select MenuItem and click Add. Then configure the menu item as see fit (Text, (Name), etc).

Like any menu item, each sub-menu item is an object of type ToolStripMenuItem. Therefore, to programmatically create a sub-menu, create each ToolStripMenuItem item and add it to the ToolStripMenuItem menu item that will act as its parent.

 

Previous Copyright © 2007-2012 FunctionX Next