Sign in Join
Wiki copytemporary

TPopupMenu

Deutsch · English · Français · Suomi · Русский · 日本語

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.

To see the Menu Editor, right-click on the Main Menu icon on your Form.

Creating a PopupMenu

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

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

pascal
procedure TForm1.popLoadClick(Sender: TObject);
begin

end;

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

pascal
procedure TForm1.popLoadClick(Sender: TObject);
begin
  if OpenDialog1.Execute then                            //only if a file is selected
    try                                                  //try
      Image1.Picture.LoadFromFile(OpenDialog1.Filename); //to load that file
    except
    end;
end;
pascal
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;

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:

pascal
procedure TForm1.Button1Click(Sender: TObject);
begin
  PopupMenu1.PopUp;   //shop PopupMenu
end;

Reusing a popup menu

The same popup menu can be used by multiple controls using PopupComponent to retrieve the caller of the popup.

pascal
...
Uses 
... Clipbrd...

procedure TfrmIniPrevMain.pmnuMenuItem1Click(Sender: TObject);
begin
  if pmnuMenuItem1.PopupComponent.ClassType = TMemo then
  begin
    Clipboard.AsText := TMemo(pmnuClipBoard.PopupComponent).Text;
  end;
  if pmnuClipBoard.PopupComponent.ClassType = TEdit then
  begin
    Clipboard.AsText := TEdit (pmnuClipBoard.PopupComponent).Text;
  end;
end;

See also


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.

Revision 2, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.