Sign in Join
Wiki copytemporary

TPopupMenu: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: TPopupMenu

OldNewLine
1+A **TPopupMenu** ![](/wiki-images/e/e4/Tpopupmenu.png) is a menu panel that pops up on the desktop when the right mouse button is clicked.
2+
3+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.
4+
5+To see the Menu Editor, right-click on the Main Menu icon on your Form.
6+
7+## Creating a PopupMenu
8+
9+In the next passage, I will show you, how to assign a popup menu to a component an your [[TForm|Form]]:
10+
11+- Create a new [[Form Tutorial|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*.
12+- 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).
13+- 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 *PopupMenu1*is displayed on the form (a square with a representation of a drop down menus and the name of the component).
14+- Right click now on "PopupMenu1", a pop-up menu appears. Click the first entry **Menu Editor**.
15+- 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.
16+- 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.
17+- We want a better caption than New Item1, so go to the Caption property and type in **Load** and press enter.
18+- 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.
19+- 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)**.
20+- Change caption to **Centered** and name to **popCenter**.
21+- Procedure the menu editor so each menu item which you need and close at the end.
22+- 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.
23+- Select on the form *Image1* and go in the object Inspector on the property **PopupMenu**. There, select *PopupMenu1* in the adjacent combobox.
24+- In run-time, the popup menu will always appear if you right click the image.
25+
26+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 [[Event order|events]] for each menu that is to react to being clicked upon.
27+
28+The following explains how to edit the events of menu clicks using the Object Inspector.
29+
30+## Making the menu actually do something
31+
32+- 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 [[Event order|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:
33+
34+
35+``` pascal
36+procedure TForm1.popLoadClick(Sender: TObject);
37+begin
38+
39+end;
40+```
41+
42+- Between the statements **begin** and **end** you can now insert the code for the menu entry click *Load*.
43+
44+In our case we could insert a TOpenDialog control on the form and use the standard dialog for our purposes:
45+
46+- For that, put a [[TOpenDialog]] control (component palette dialog) on your form, with the name *OpenDialog1*. Now change your procedure to:
47+
48+``` pascal
49+procedure TForm1.popLoadClick(Sender: TObject);
50+begin
51+ if OpenDialog1.Execute then //only if a file is selected
52+ try //try
53+ Image1.Picture.LoadFromFile(OpenDialog1.Filename); //to load that file
54+ except
55+ end;
56+end;
57+```
58+
59+- Analogous procedures with other menu items and create their event handlers and insert following code:
60+
61+``` pascal
62+procedure TForm1.popStretchClick(Sender: TObject);
63+begin
64+ popStretch.Checked := not popStretch.Checked; //mark/checked on/off
65+ Image1.Stretch := popStretch.Checked; //Image1 streched yes/no
66+end;
67+
68+procedure TForm1.popCenterClick(Sender: TObject);
69+begin
70+ popCenter.Checked := not popCenter.Checked; //mark/checked on/off
71+ Image1.Center := popCenter.Checked; //Image1 center yes/no
72+end;
73+```
74+
75+- Now you can start the sample with <span class="kbd">F9</span>, 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.
76+
77+## Own popup
78+
79+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`**.
80+
81+A simple example:
82+
83+- Create a new [[Form Tutorial|GUI application]] and add a TPopupMenu *PopupMenu1* and a [[TButton]] *Button1* to your form.
84+- Insert a few menu entries in *PopupMenu1* (see [[TPopupMenu|Creating a PopupMenu]]).
85+- Generate the event handler for the OnClick event of *Button1* and write the following code:
86+
87+``` pascal
88+procedure TForm1.Button1Click(Sender: TObject);
89+begin
90+ PopupMenu1.PopUp; //shop PopupMenu
91+end;
92+```
93+
94+- Start your program, the popup menu is called by clicking the button.
95+
96+## Reusing a popup menu
97+
98+The same popup menu can be used by multiple controls using *PopupComponent* to retrieve the caller of the popup.
99+
100+``` pascal
101+...
102+Uses
103+... Clipbrd...
104+
105+procedure TfrmIniPrevMain.pmnuMenuItem1Click(Sender: TObject);
106+begin
107+ if pmnuMenuItem1.PopupComponent.ClassType = TMemo then
108+ begin
109+ Clipboard.AsText := TMemo(pmnuClipBoard.PopupComponent).Text;
110+ end;
111+ if pmnuClipBoard.PopupComponent.ClassType = TEdit then
112+ begin
113+ Clipboard.AsText := TEdit (pmnuClipBoard.PopupComponent).Text;
114+ end;
115+end;
116+```
117+
118+## See also
119+
120+- [[doc:lcl/menus/tpopupmenu.html|TPopupMenu doc]]
121+- [[TMainMenu]]
122+
123+---
124+*From the Free Pascal wiki, page "TPopupMenu", as it stood on 2023-07-31 after a change by Paskal, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history