Sign in Join
Wiki copytemporary

TPopupMenu: what changed

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

OldNewLine
11A **TPopupMenu** ![](/wiki-images/e/e4/Tpopupmenu.png) is a menu panel that pops up on the desktop when the right mouse button is clicked.
22
33It 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.
44
55To see the Menu Editor, right-click on the Main Menu icon on your Form.
66
77## Creating a PopupMenu
88
99In the next passage, I will show you, how to assign a popup menu to a component an your [[TForm|Form]]:
1010
1111- 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*.
1212- 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).
1313- 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).
1414- Right click now on "PopupMenu1", a pop-up menu appears. Click the first entry **Menu Editor**.
1515- 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.
1616- 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.
1717- We want a better caption than New Item1, so go to the Caption property and type in **Load** and press enter.
1818- 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.
1919- 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)**.
2020- Change caption to **Centered** and name to **popCenter**.
2121- Procedure the menu editor so each menu item which you need and close at the end.
2222- 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.
2323- Select on the form *Image1* and go in the object Inspector on the property **PopupMenu**. There, select *PopupMenu1* in the adjacent combobox.
2424- In run-time, the popup menu will always appear if you right click the image.
2525
2626Now, 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.
2727
2828The following explains how to edit the events of menu clicks using the Object Inspector.
2929
3030## Making the menu actually do something
3131
3232- 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:
3333
3434
3535``` pascal
3636procedure TForm1.popLoadClick(Sender: TObject);
3737begin
3838
3939end;
4040```
4141
4242- Between the statements **begin** and **end** you can now insert the code for the menu entry click *Load*.
4343
4444In our case we could insert a TOpenDialog control on the form and use the standard dialog for our purposes:
4545
4646- For that, put a [[TOpenDialog]] control (component palette dialog) on your form, with the name *OpenDialog1*. Now change your procedure to:
4747
4848``` pascal
4949procedure TForm1.popLoadClick(Sender: TObject);
5050begin
5151 if OpenDialog1.Execute then //only if a file is selected
5252 try //try
5353 Image1.Picture.LoadFromFile(OpenDialog1.Filename); //to load that file
5454 except
5555 end;
5656end;
5757```
5858
5959- Analogous procedures with other menu items and create their event handlers and insert following code:
6060
6161``` pascal
6262procedure TForm1.popStretchClick(Sender: TObject);
6363begin
6464 popStretch.Checked := not popStretch.Checked; //mark/checked on/off
6565 Image1.Stretch := popStretch.Checked; //Image1 streched yes/no
6666end;
6767
6868procedure TForm1.popCenterClick(Sender: TObject);
6969begin
7070 popCenter.Checked := not popCenter.Checked; //mark/checked on/off
7171 Image1.Center := popCenter.Checked; //Image1 center yes/no
7272end;
7373```
7474
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.
75+- 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.
7676
7777## Own popup
7878
7979Maybe 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`**.
8080
8181A simple example:
8282
8383- Create a new [[Form Tutorial|GUI application]] and add a TPopupMenu *PopupMenu1* and a [[TButton]] *Button1* to your form.
8484- Insert a few menu entries in *PopupMenu1* (see [[TPopupMenu|Creating a PopupMenu]]).
8585- Generate the event handler for the OnClick event of *Button1* and write the following code:
8686
8787``` pascal
8888procedure TForm1.Button1Click(Sender: TObject);
8989begin
9090 PopupMenu1.PopUp; //shop PopupMenu
9191end;
9292```
9393
9494- Start your program, the popup menu is called by clicking the button.
9595
9696## Reusing a popup menu
9797
9898The same popup menu can be used by multiple controls using *PopupComponent* to retrieve the caller of the popup.
9999
100100``` pascal
101101...
102102Uses
103103... Clipbrd...
104104
105105procedure TfrmIniPrevMain.pmnuMenuItem1Click(Sender: TObject);
106106begin
107107 if pmnuMenuItem1.PopupComponent.ClassType = TMemo then
108108 begin
109109 Clipboard.AsText := TMemo(pmnuClipBoard.PopupComponent).Text;
110110 end;
111111 if pmnuClipBoard.PopupComponent.ClassType = TEdit then
112112 begin
113113 Clipboard.AsText := TEdit (pmnuClipBoard.PopupComponent).Text;
114114 end;
115115end;
116116```
117117
118118## See also
119119
120120- [[doc:lcl/menus/tpopupmenu.html|TPopupMenu doc]]
121121- [[TMainMenu]]
122122
123123---
124124*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