Deutsch (de) English (en) suomi (fi) français (fr) русский (ru)

A TPopupMenu is a menu panel that pops up on the desktop when the right mouse button is clicked.

It is a non-visible component: that is, if the icon is selected from the Standard tab of the Component Palette and placed on the form, it will not appear at run-time. Instead, a menu bar with a structure defined by the menu editor will appear.

## Contents

In the next passage, I will show you, how to assign a popup menu to a component an your Form:

• Create a new GUI application and insert a TImage to the form. To do this, choose the TImage from component palette Additional and click on your form. It will put a TImage on your form, with the name Image1.
• Go to the Object Inspector (which must be on Image1) and under the tab properties, select the Align property. Adjust the alClient align in the adjacent combobox (the Image1 is adjusted in the size of the form).
• Now you put a TPopupMenu (component tab standard) on your form (if possible on the "Image1", so you know that this is one of the image). The component PopupMenu1is displayed on the form (a square with a representation of a drop down menus and the name of the component).
• The Menu Editor window will open with a menu item already created with a caption of New Item1. Probably you want to change it, so click on it and go to the Object Inspector.
• In Object Inspector, change the Name property from MenuItem1 to something more appropriate. Let's say this is the Load menu, so let's change Name by typing in popLoad and press enter.
• We want a better caption than New Item1, so go to the Caption property and type in Load and press enter.
• Now we want to create yet another menu entry. Go back to the Menu Editor window. Rightclick on Load. A pop-up menu will appear. Click on Insert New Item After, and a new menu, called New Item2 will appear. As explained in the last two items, let's change its name to popStretch and the caption to Stretched in the Object Inspector.
• We noticed that we forgot a menu entry. That's not bad, you go back in the menu editor. You can either right-click on Load and click on Insert New Item (after) or right-click on Stretched and click on Insert New Item (before).
• Change caption to Centered and name to popCenter.
• Procedure the menu editor so each menu item which you need and close at the end.
• When you have added all the menus, you must still set what component it should be associated with. In our case, this should be the TImage Image1, what we have placed at the beginning at the form.
• Select on the form Image1 and go in the object Inspector on the property PopupMenu. There, select PopupMenu1 in the adjacent combobox.
• In run-time, the popup menu will always appear if you right click the image.

Now, all this will get you is a menu that displays at run time and will allow the user to click on the menus. It won't actually do anything. To have the menu items do something, you have to add events for each menu that is to react to being clicked upon.

The following explains how to edit the events of menu clicks using the Object Inspector.

## Making the menu actually do something

• Go back to the menu editor and select the Load menu item with a click. Now go to the Object Inspector window, and select the tab events. The only event that you really want to change, is OnClick, which is currently empty. If you already have an existing EventHandler to use, can use these and choose accordingly, otherwise you can create one by Lazarus. There is a button with 3 dots on the right side. Click on it, and a new procedure (your newly created eventhandler) is created in the code and the view changes to the source text editor. Your Procdure will look something like this:
procedure TForm1.popLoadClick(Sender: TObject);
begin

end;
• Between the statements begin and end you can now insert the code for the menu entry click Load.

In our case we could insert a TOpenDialog control on the form and use the standard dialog for our purposes:

• For that, put a TOpenDialog control (component palette dialog) on your form, with the name OpenDialog1. Now change your procedure to:
procedure TForm1.popLoadClick(Sender: TObject);
begin
if OpenDialog1.Execute then                            //only if a file is selected
try                                                  //try
except
end;
end;
• Analogous procedures with other menu items and create their event handlers and insert following code:
procedure TForm1.popStretchClick(Sender: TObject);
begin
popStretch.Checked := not popStretch.Checked;  //mark/checked on/off
Image1.Stretch := popStretch.Checked;          //Image1 streched yes/no
end;

procedure TForm1.popCenterClick(Sender: TObject);
begin
popCenter.Checked := not popCenter.Checked;  //mark/checked on/off
Image1.Center := popCenter.Checked;          //Image1 center yes/no
end;
• Now you can start the sample with F9, open the popup menu with a right click and click on Load. Now, load a picture of your choice. If you have loaded an image, try the other menu entries.

## Own popup

Maybe you do not want to have displayed a popup menu with a right click on a specific component, but at a different event. That you can realize with PopupMenu.PopUp.

A simple example:

procedure TForm1.Button1Click(Sender: TObject);
end;