Home

Popup Menus

 

Popu Menus

 

Introduction

A popup menu is one that appears when the user right-clicks an area of an application or form. In most applications, when the user right-clicks a title bar, the operating system is configured to display a system menu. Here is an example:

 

Operating system's title bar menu

 

A menu is considered, or qualifies as, popup if, or because, it can appear anywhere on the form as the programmer wishes. Such a menu is also referred to as context-sensitive or contextual because its appearance and behavior depend on where it displays on the form or on a particular control. The person who creates the application decides if or where the contextual menu would appear. Because this characteristic is up to the programmer, the same application can display different types of popup menus depending on where the user right-clicks. Here are examples:

Context-sensitive menu

Contextual menu

The first difference between a main menu and a popup menu is that a popup menu appears as one category or one list of items and not like a group of categories of menus like a main menu. Secondly, while a main menu by default is positioned on the top section of a form, a popup menu doesn't have a specific location on the form.

Practical LearningPractical Learning: Introducing Contextual Menus

  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

Creating a Popup Menu

To support the creation and management of popup menus, the VCL provides the TPopupMenu class. This class is derived from TMenu.

To visually create a contextual menu, in the Standard section of the Tool Palette, click the TPopupMenu button Popup Menu and click the form. Once you have a TPopupMenu object, you can create its menu items. To do this, as mentioned for the TMainMenu, right-click the TPopupMenu object and click Menu Designer... Configure the items using the Object Inspector.

To programmatically create a popup menu, declare a variable of type TPopupMenu. Specify its owner in its constructor. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    TPopupMenu *mnuContext = new TPopupMenu(this);
}
//---------------------------------------------------------------------------

Unlike a main menu, a popup menu provides a single list of items. If you want different popup menus for your form, you have two options. You can create various popup menus or programmatically change your single popup menu in response to something or some action on your form.

Each item of a popup menu is an object of type TMenuItem. Use it as reviewed before to create a menu item. After creating a menu item, to add it to the popup menu, use its Items property that is equipped with an Add Method. Here are examples:

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

	mnuFileNew->Caption = L"New";
	mnuContext->Items->Add(mnuFileNew);
	mnuFileOpen->Caption = L"Open";
	mnuContext->Items->Add(mnuFileOpen);
}
//--------------------------------------------------------------------------

Practical LearningPractical Learning: Creating a Popup Menu

  1. In the lower section, click Design
  2. From the Standard section of the Tool Palette, click TPopupMenu TPopupMenu and click the form
  3. WWhile the popup menu object is still selected, in the Object Inspector, click Name and type mnuWithProperties and press Enter
  4. On the form, double-click mnuWithProperties
  5. In the Menu Designer, as a default menu item is selected, in the Object Inspector, click Caption and type Edit
  6. Click Name and type mnuEditProperty
  7. In the Menu Designer, click the box under Edit
  8. In the Object Inspector, click Caption and type Delete
  9. Click Name and type mnuDeleteProperty
  10. In the Menu Designer, click the box under Delete
  11. In the Object Inspector, click Caption and type Clear
  12. Click Name and type mnuClearProperties and press Enter
     
    Menu Designer
  13. In the Standard section of the Tool Palette, click PopupMenu and click the form (the Menu Designer should still be displaying)
  14. While the popup menu is still selected, in the Object Inspector click Name and type mnuNoProperty and press Enter
  15.  (The Menu Designer should still be displaying).
    On the Menu Designer, click the defaultly created box
  16. In the Object Inspector, click Caption
  17. Type New Property and press Enter
  18. Click Name, type mnuNewProperty and press Enter
  19. Close the Menu Designer

Using a Popup Menu

After creating a popup menu, in order to use it, you must assign it to the control that will use it. To support this, the Window controls are equipped with a property named PopupMenu. Assign the TPopupMenu object to this property. Here is an example:

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

    mnuFileNew->Caption = L"New";
    mnuContext->Items->Add(mnuFileNew);
    mnuFileOpen->Caption = L"Open";
    mnuContext->Items->Add(mnuFileOpen);

    pnlSomething->PopupMenu = mnuContext;
}
//---------------------------------------------------------------------------

This would produce:

Popup Menu

Practical LearningPractical Learning: Creating a Context Menu

  1. In the Standard section of the Tool Palette, click TPanel Panel and click the form (no need for precision)
  2. While the panel is still selected on the form, in the Object Inspector, click PopupMenu, click the arrow of the box and select mnuWithProperties
  3. In the Standard section of the Tool Palette, click TListBox Panel and click the form away from the panel (no need for precision)
  4. In the Object Inspector, click PopupMenu, click the arrow of the box and select mnuNoProperty
  5. To test the application, press F9
  6. Right-click the panel
     
    Menu
  7. Right-click the other control
     
    Menu
  8. Close the form and return to your programming environment

Coding a Popup Menu

In your application, you can create as many popup menus as you want. If you have different controls, each can have its own popup menu or many can share a popup menu. Also, you can use different contextual menus for a control and decide what menu to display when/why.

There is nothing particularly specific with writing code for a popup menu item. You approach it exactly as if you were dealing with a menu item of a main menu. You can write code for an item of a popup menu independent of any other item of a main menu. If you want an item of a popup menu to respond to the same request as an item of a main menu, you can write code for one of the menu items (either the item on the main menu or the item on the popup menu) and simply call its OnClick event in the event of the other menu item.

 
 
 

Home Copyright © 2010-2016, FunctionX