How to create a button with a drop-down menu?

Is there a way to show IE / Firefox Back button style dropdown menu?

+6
button delphi popupmenu
source share
4 answers

I assume that you mean a button that throws a menu when pressed.

You can also simply manually enter the code by clicking the mouse button to release TPopupMenu under it.

An example . Put something with TClickEvent (possibly TButton) and TPopupMenu in your form. Add some menu items. Then add the following OnClick event handler:

procedure TForm86.Button1Click(Sender: TObject); var button: TControl; lowerLeft: TPoint; begin if Sender is TControl then begin button := TControl(Sender); lowerLeft := Point(button.Left, button.Top + Button.Height); lowerLeft := ClientToScreen(lowerLeft); PopupMenu1.Popup(lowerLeft.X, lowerLeft.Y); end; end; 

And viola! Just like magic. You can wrap it all in a component if you plan to reuse it. Perhaps even sell it online.

Note. . If you need a delay, then extract this code in another way, then set the OnClick timer and enable the OnMouseLeave timer. Then, if the timer fires, you can call the extracted method. Not sure how you do it when you press the keyboard. I do not know if Firefox supports etc.

+10
source share

Jim's answer is great, but at first it didn't work for me. ClientToScreen uses the Form86 method, which is only correct if the button is directly on the form. This should be a called ClientToScreen button method, for example:

 procedure TForm86.Button1Click(Sender: TObject); var button: TControl; lowerLeft: TPoint; begin if Sender is TControl then begin button := TControl(Sender); lowerLeft := Point(0, button.Height); lowerLeft := button.ClientToScreen(lowerLeft); PopupMenu1.Popup(lowerLeft.X, lowerLeft.Y); end; end; 

This works no matter where the button is located.

+8
source share

Of course. Place the toolbar on the page. Right click on the toolbar, add a button. Set the button style to tbsDropDown. Put PopupMenu on the page. Double-click PopupMenu to define menu items. Then go back to the button you created and set the DropdownMenu property to indicate the PopupMenu you just created.

+6
source share

If you do not want to use the toolbar, the raize (www.raize.com) and express editors (www.DevExpress.com) libraries have components that can do this.

+1
source share

All Articles