Overview
In Delphi, we create menus to enhance the usability of our software and facilitate such tasks as opening a registration screen, creating reports, closing our program, etc.
The TMainMenu component is one of the resources available in Delphi for creating menus for our forms.
You can also check out our Delphi development services
Let’s have a look at how it is possible to create the main menu for your Delphi applications using TMainMenu. Let’s analyze how to manipulate and configure this component and how we can create a build menu for software developed in Delphi.
The Menu Designer enables you to quickly build any menu. The menu structure for the main menu consists of a MainMenu component, which is represented by the VCL class TMainMenu. Each item on the menu is a MenuItem component that is encapsulated in the TMenuItem class. You don’t need to be too concerned about the intricacies of how these classes work together because the Menu Designer makes the process of creating menus really easy.
Adding TMainMenu
To add a TMainMenu component, find it on the Standard palette. Select it and drag it to your form.
To access the Items property, double-click on the component or find it in the Object Inspector
The Caption property is used to add text to the menu item
When we start running the project, we will see the text that we’ve typed in the caption right at the top of the form
Adding Items to TMainMenu
To add an item select “Test” and a new item will be added automatically
Adding Mnemonic
When we open Delphi’s File menu, we find some options there, like File -> New, Open, Reopen, Save. Take a look at the image placed below to understand better what we are talking about.
For example, we can execute the Open menu item by, using the hotkeys:
ALT + F -> Access the File menu and open it;
O -> Execute the Open item in the File menu.
We can also do this by using the MainMenu component.
Using the symbol “&” before the Item name we could add a mnemonic element to the menu item
Adding a Separator
By using the symbol “-” in the Caption of an item, we create a separator for the menu
Adding Hotkeys
The shortcut keys help to enhance the usability of our software, as thanks to them, we can quickly open a certain screen. To do it, it will be enough just to press one key or a simple combination of keys. For example, CTRL+S in the Delphi IDE (as shown in Figure 1) can be used to run the menu Save
Adding an Image to an Item
These days we greatly focus on the layout of our applications and the menu shouldn’t be an exception. An image can help to identify and even visually memorize a particular option. We can add an image through the Bitmap option of a certain item in our Menu
Creating a Submenu
We can create a Submenu within a menu, just as we can create Submenus within items of other Submenus, thus building a hierarchy for the entire menu.
Marking an Item
As we can see, we have marked “Item 3”. This functionality is available through the Checked property of this item.
To be able to simulate it, we must mark the Checked property as TRUE. The problem is that it will be always checked. And to make it possible to “check” (by making one click) and “uncheck” items (with a second click) we need to perform this task programmatically, using the following code:
With this implementation, if ClientesAtivo1 has its property Checked = True, it can be possible to change it to False and, when you click on it again, the effect will be the opposite.
Check out our video on how to create a menu in Delphi