Jump to content
Incus J

TButton : How to display the assigned Popup Menu?

Recommended Posts

I've created a TPopupMenu, and assigned it to the PopupMenu property of a TButton.  When the button is left clicked I'd like the assigned PopupMenu to appear just below the button.

 

The documentation suggests "Use PopupMenu to set the context menu of the current control. The pop-up window is displayed when ShowContextMenu is called."

 

In the button OnClick handler, I've added :

procedure TmyForm.myButtonClick(Sender: TObject);
begin
  myButton.ShowContextMenu;
  end;

This doesn't compile :

[dccosx64 Error] myForm.pas(73): E2362 Cannot access protected symbol TControl.ShowContextMenu

The procedure ShowContextMenu seems to be Protected.  Perhaps it is not meant to be called directly, but I haven't spotted an auto-popup option yet.  ShowContextMenu takes coordinates, but I'd like FMX to decide the best place to display the PopupMenu for me, rather than me having to specify coordinates, because the form can be resized or maximised, and the width of the PopupMenu may vary depending on the length of the menu items, so the ideal position is likely to be variable.

 

How do I get a button to show its assigned popup menu (in a sensible position) when left clicked?

Edited by Incus J

Share this post


Link to post

Hi Virgo - Yes, it pops up OK on right click (but the menu tends to overlap the button, obscuring it).  I've come up with this for left click :

procedure TmyForm.myButtonClick(Sender: TObject);
var
  h:single;
  pt:TPointF;
begin
  h:=myButton.Height;
  pt:=myButton.LocalToAbsolute(PointF(0,h));
  pt:=Self.ClientToScreen(pt);
  myButton.PopupMenu.Popup(pt.x,pt.y);
  end;

...though it seems a bit convoluted, and I'm not sure how robust it is.

Share this post


Link to post

On right click it probably pops up at the location of mouse cursor... That's what it is intended to do, like all context menus.

Share this post


Link to post

But what about TButton.DropDownMenu ? I think, that this is intended for what you are trying to achieve.

Edited by Virgo

Share this post


Link to post

Yes, I'm probably trying to use it for something other than intended (this happens to me quite a lot).  Still, click a button for a menu to appear, must be a fairly common requirement.  I'll see whether FMX offers another control better suited for this.

 

Ah - I've just seen your last reply, thank you - I will take a look.

Share this post


Link to post

TButton.DropDownMenu looks to be the right thing to use for a VCL application.  The FMX TButton doesn't have that property though, so perhaps FMX lacks this capability.

Share this post


Link to post
5 minutes ago, Incus J said:

TButton.DropDownMenu looks to be the right thing to use for a VCL application.  The FMX TButton doesn't have that property though, so perhaps FMX lacks this capability.

Right... Did not check the FMX part.

Share this post


Link to post

I'll go with the above code for now, until I find a better way to do it.  I'm just trying to figure out how to calculate the width of the Popup Menu before it appears, so I can align it centrally or right-aligned.  At the moment it displays left aligned (to the left edge of the button) - right aligned to the right edge of the button would be particularly useful as the button is positioned at the very right hand side of the form.

 

I could do with a property like :

PopupMenu.Width; //no such property

TMenuItem has CalcSize and CalcRenderSize so perhaps I could iterate over the Items to determine a value for the widest Menu Item.

Edited by Incus J

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×