Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How add a standard action to a ActnPopup.TPopupActionBar component in runtime?

i'm using the ActnPopup.TPopupActionBar component and I want add a couple of standard actions like TFileOpen, TFileOpenWith and so on. The question is how i can add these actions in runtime to an TPopupActionBar?

like image 492
Salvador Avatar asked Mar 12 '12 22:03

Salvador


1 Answers

I would try something like this:

procedure TForm1.Button1Click(Sender: TObject);
var
  MenuItem: TMenuItem;
  Action: TCustomAction;
begin
  MenuItem := TMenuItem.Create(PopupActionBar1);
  Action := TFileOpen.Create(PopupActionBar1);
  Action.Caption := '&Open...';
  Action.ShortCut := 16463;
  MenuItem.Action := Action;
  PopupActionBar1.Items.Add(MenuItem);

  MenuItem := TMenuItem.Create(PopupActionBar1);
  Action := TFileOpenWith.Create(PopupActionBar1);
  Action.Caption := 'Open with...';
  MenuItem.Action := Action;
  PopupActionBar1.Items.Add(MenuItem);
end;

And where did I get the Caption and ShortCut values ? Good question. These are from the action list's component editor. You can get them if you add the standard actions to your action list and look into your form's source code. There you will see your action definitions, like this one:

object FileOpen1: TFileOpen
  Category = 'File'
  Caption = '&Open...'
  Hint = 'Open|Opens an existing file'
  ImageIndex = 7
  ShortCut = 16463
end

And since for popup menu you don't need a Hint (for popup menu item ?), Category (is for action list) nor ImageIndex (you may define your own image set, thus your indexes might be different), you can leave them. In fact you can leave all of this, the action will be performed even so (based on the class you'll use), but you would have no caption nor shortcut.

like image 75
TLama Avatar answered Sep 28 '22 07:09

TLama