Home

Visual Assistance With Menu Items

 

Visual Assistance With Menu Items

 

Introduction

After creating a menu (main menu and popup 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.

Practical LearningPractical Learning: Introducing Menu Assistance

  1. Start Embarcadero RAD Studio
  2. To create a new application, on the main menu, click File -> New -> VCL Forms Application - C++Builder
  3. In the Tool Palette, expand the Standard section if necessary. Click the TMainMenu control Main Menu and click the form
  4. While the main menu object is still selected on the form, in the Object Explorer, click Name and type mnuMain
  5. On the form, right-click mnuMain and click Menu Designer
  6. In the Menu Designer, make sure an item is selected. Otherwise, click the dark blue box in the top-left section.
    In the Object Explorer, click Caption
  7. Type File
  8. Click Name and type mnuFile
  9. In the Menu Designer, click the box under File
  10. In the Object Inspector, click Caption and type New Property
  11. Click Name and type mnuFileNewProperty
  12. In the Menu Designer, click the box under New Property
  13. In the Object Inspector, click Caption and type Exit
  14. Click Name
  15. Type mnuFileExit and press Enter
  16. Click the Close button of the Menu Designer to close it
  17. On the main menu, click File -> New -> Form - C++Builder
  18. In the Object Inspector, click Name
  19. Type RealEstateProperty and press Enter
  20. To display the first form, in the top section, Unit1.cpp
  21. On the main menu, click File -> Use Unit...
  22. In the Use Unit dialog box, click Unit2.cpp and click OK
  23. On the form, double-click mnuMain
  24. In the Menu Designer, click File and double-click New Property
  25. Implement the event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TForm1::mnuFileNewPropertyClick(TObject *Sender)
    {
    	RealEstateProperty->ShowModal();
    }
    //---------------------------------------------------------------------------
  26. On the Menu Designer, double-click Exit
  27. Close the Menu Designer
  28. Implement the event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TForm1::mnuFileExitClick(TObject *Sender)
    {
    	Close();
    }
    //---------------------------------------------------------------------------
  29. To build and execute, on the main menu, click Run -> Run
  30. On the main menu of the form, click File -> New Property
     
    Using a Main Menu Item
  31. After viewing the form, close it
  32. To close the main form, click File -> Exit

Access Keys

A menu item can display an 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 F10 key, then F. 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:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	TMainMenu *mnuMain = new TMainMenu(this);
	TMenuItem *mnuFile = new TMenuItem(this);
	TMenuItem *mnuFileNew = new TMenuItem(this);
	TMenuItem *mnuFileOpen = new TMenuItem(this);

	mnuFile->Caption = L"&File";
	mnuFileNew->Caption = L"&New";
	mnuFile->Add(mnuFileNew);

	mnuFileOpen->Caption = L"&Open";
	mnuFile->Add(mnuFileOpen);

	mnuMain->Items->Add(mnuFile);
}
//---------------------------------------------------------------------------

 

After creating the menu, to use it, the user can press Alt or F10:

Access Keys

Practical LearningPractical Learning: Using Access Keys

  1. On the form, right-click mnuMain and click Menu Designer...
  2. On the Menu Designer, click File
  3. In the Object Inspector, click Caption and edit it as follows &File
  4. In the Menu Designer, click New Property
  5. In the Object Inspector, click Caption and change it to &New Property
  6. In the Menu Designer, click Exit
  7. In the Object Inspector, click Caption and put & between E and x to have E&xit

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 ShortCut property.

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

Shortcut

To specify a shortcut, select from that list.

To programmatically create a short key, call the ShortCut() function. Its syntax is:

TShortCut ShortCut(Word Key, TShiftState Shift);

This function takes two arguments. The Key argument is a letter passed as a Word. The second argument specifies whether you will use the Ctrl key, the Shift key, or a combination of a Ctrl + Shift key. To provide this value, apply the << operator to the TShiftState() function. Specify the Shift key as ssShift or the Ctrl key as ssCtrl. Here are two examples:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	TMainMenu *mnuMain = new TMainMenu(this);
	TMenuItem *mnuFile = new TMenuItem(this);
	TMenuItem *mnuFileNew = new TMenuItem(this);
	TMenuItem *mnuFileOpen = new TMenuItem(this);

	mnuFile->Caption = L"&File";
	mnuFileNew->Caption = L"&New";
	mnuFileNew->ShortCut = ShortCut(Word(L'N'), TShiftState() << ssCtrl);
	mnuFile->Add(mnuFileNew);

	mnuFileOpen->Caption = L"&Open";
	mnuFileOpen->ShortCut = ShortCut(Word(L'O'), TShiftState() << ssCtrl);
	mnuFile->Add(mnuFileOpen);

	mnuMain->Items->Add(mnuFile);
}
//---------------------------------------------------------------------------

This would produce:

Short Cuts

Practical LearningPractical Learning: Creating Shortcuts

  1. In the Menu Designer, click Exit
  2. In the Object Inspector, click ShortCut and click the arrow of its combo box
  3. In the window that appears, select Ctrl+X
     
    ShortCut Menu
 

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

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	TMainMenu *mnuMain = new TMainMenu(this);
	TMenuItem *mnuFile = new TMenuItem(this);
	TMenuItem *mnuFileNew = new TMenuItem(this);
	TMenuItem *mnuFileOpen = new TMenuItem(this);

	TMenuItem *mnuTools = new TMenuItem(this);
	TMenuItem *mnuToolsOptions = new TMenuItem(this);

	mnuFile->Caption = L"&File";
	mnuFileNew->Caption = L"&New";
	mnuFileNew->ShortCut = ShortCut(Word(L'N'), TShiftState() << ssCtrl);
	mnuFile->Add(mnuFileNew);

	mnuFileOpen->Caption = L"&Open";
	mnuFileOpen->ShortCut = ShortCut(Word(L'O'), TShiftState() << ssCtrl);
	mnuFile->Add(mnuFileOpen);

	mnuMain->Items->Add(mnuFile);

	mnuTools->Caption = L"&Tools";
	mnuToolsOptions->Caption = L"&Options...";
	mnuTools->Add(mnuToolsOptions);

	mnuMain->Items->Add(mnuTools);
}
//---------------------------------------------------------------------------

This would produce:

Three Periods

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.

Practical LearningPractical Learning: Adding Periods to a Menu Item

  1. In the Menu Designer, click New Property
  2. In the Object Inspector, click Caption, press End, and type ... to get New Property...
  3. On the main menu, click File -> Close All
  4. When asked whether you want to save, click No
 
 
 

Home Copyright © 2010-2016, FunctionX