Home

Windows Applications Menus:
Visual Assistance With Menu Items

 

Introduction

After creating a menu (main menu and contextual menu), there are various actions you can perform to improve it and there are many ways you can enhance the user's experience with your application. Menus provide various features such as access keys and shortcuts. There are also other things you can do such as grouping menus. Although some of these actions are not required to make an application useful, they can be necessary to make it more professional.

Access Keys

You may notice that some menu items have a letter underlined. Using this letter allows the user to access the menu using a keyboard. For example, if the letter F is underline in a File menu as in File, the user can access the File menu by pressing the Alt, then the F keys. To create this functionality, choose a letter on the menu item and precede it with the & character. For example, &File would produce File. You can apply the same principle if you are programmatically creating the menu. Here are two examples:

#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:
	ToolStripMenuItem ^ mnuFile;
	ToolStripMenuItem ^ mnuNew;

public:
    CExercise()
    {
	InitializeComponent();
    }

    void InitializeComponent()
    {
	mnuFile = gcnew ToolStripMenuItem(L"&File");
	mnuNew  = gcnew ToolStripMenuItem(L"&New");
    }
};

Shortcuts

A shortcut is a key or a combination of keys that the user can press to perform an action that can also be performed using a menu item. When creating a menu, to specify a shortcut, use the ShortcutKeys property.

To visually specify a shortcut, in the menu designer, click the menu item. In the Properties window, click ShortcutKeys and click the arrow of the field, a window would come up:

To specify just a letter for the shortcut, you can click the arrow of the combo box on the left side of the Reset button. A list would come up from which you can select the desired letter:

You are probably more familiar with shortcuts made of combinations of keys, such as Ctrl + N, Alt + F6, or Ctrl + Alt + Delete. To visually create such a shortcut, click the check box(es) and select the desired letter.

If you have used applications like Microsoft Word or Adobe Photoshop, you may know that they don't show all of their shortcuts on the menu. If you want to hide a shortcut, after specifying it, in the Properties window, set the ShowShortcutKeys property to False.

To programmatically specify a shortcut, assign a key or a combination of keys to the ShortcutKeys property of the ToolStripMenuItem class. The ShortcutKeys property is of type Keys, which is an enumeration of the various keys of a keyboard recognized by Microsoft Windows. Here is an example:

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

public:
    CExercise()
    {
	InitializeComponent();
    }

    void InitializeComponent()
    {
	mnuMain = gcnew MenuStrip;
	mnuFile = gcnew ToolStripMenuItem(L"&File");
	mnuFileNew = gcnew ToolStripMenuItem(L"&New");

	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(mnuFileExit);
	mnuMain->Items->Add(mnuFile);

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

	Controls->Add(mnuMain);
    }
};

This would produce:

To create a shortcut that is a combination of keys, use the bit manipulation operator OR operator represented by |. 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;
    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;

	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(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:

Normally, when you have associated a shortcut with a menu item, when the user displays the menu, the shortcut would appear. In some applications, you may want to hide the shortcut. To support this, the ToolStripMenuItem class is equipped with the Boolean ShowShortcutKeys property. The default value of this property is true. If you want to hide the shortcut, you can set this property to false.

Three Periods

When a user has clicked a menu item, an action is supposed to occur. In some cases, an intermediary action is necessary before performing or completing the action. To indicate that an intermediary action is needed for the action related to the menu item, Microsoft standards suggest that the menu's text be followed by three periods. For example, in WordPad, if you want to display the date or the time or both on a document, you must open a dialog box that would present various options for you to choose how the date/time should be displayed. To indicate that you will perform a primary action before displaying the value, the menu that leads to it shows three periods:

In this case, when you click the menu item, a dialog box would come up for you to select the desired value.

There is no programmatic relationship between the application and the menu item that displays three periods. It is only a suggestion to show them. Therefore, when creating a menu item, if you know that an intermediary action will be used to perform or complete the actual action, add three periods on the right side of its text. Here is an example:

public ref class CExercise : public Form
{
private:
    MenuStrip ^ mnuMain;
    ToolStripMenuItem ^ mnuSelect;
    ToolStripMenuItem ^ mnuSelectColor;

public:
    CExercise()
    {
	InitializeComponent();
    }

    void InitializeComponent()
    {
        mnuMain = gcnew MenuStrip;

	mnuSelect = gcnew ToolStripMenuItem(L"&Select");
	mnuSelectColor = gcnew ToolStripMenuItem(L"Background Color...");

	mnuSelect->DropDownItems->Add(mnuSelectColor);
	mnuMain->Items->Add(mnuSelect);

	Controls->Add(mnuMain);
    }
};

This would produce:

Because the three periods indicate to the user that an intermediary action will be performed, when implementing the code for the menu item, make sure you provide that intermediary action. 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 ^ mnuSelect;
	ToolStripMenuItem ^ mnuSelectColor;

	void SelectBackgroundColor(Object ^ sender, EventArgs ^ e);

public:
    CExercise()
    {
	InitializeComponent();
    }

    void InitializeComponent()
    {
        mnuMain = gcnew MenuStrip;

	mnuSelect = gcnew ToolStripMenuItem(L"&Select");
	mnuSelectColor = gcnew ToolStripMenuItem(L"&Background Color...");
	mnuSelectColor->Click +=
		gcnew System::EventHandler(this, &CExercise::SelectBackgroundColor);

	mnuSelect->DropDownItems->Add(mnuSelectColor);
	mnuMain->Items->Add(mnuSelect);

	Controls->Add(mnuMain);
    }
};

void CExercise::SelectBackgroundColor(Object ^, EventArgs ^)
{
	ColorDialog ^ dlgColor = gcnew ColorDialog;

	if( dlgColor->ShowDialog() == ::DialogResult::OK )
		BackColor = dlgColor->Color;
}

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

    return 0;
}
 

Home Copyright © 2007-2012 FunctionX Next