How to create a button with drop-down menu?

Serkan picture Serkan · Oct 25, 2008 · Viewed 18.6k times · Source

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

Answer

Jim McKeeth picture Jim McKeeth · Oct 25, 2008

I am assuming you mean a button that drops down a menu when clicked.

You can also just manually code your button click to drop down a TPopupMenu under it.

Example: Drop anything with a TClickEvent (maybe a TButton) and a TPopupMenu on 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 could wrap it all up in a component if you plan to reuse it. Maybe even sell it online.

Note: If you want a delay, then extract that code in another method and then set a timer OnClick and turn the timer of OnMouseLeave. Then if the timer fires you can call the extracted method. Not sure how you would do it on a keyboard click. I don't know if Firefox, etc. supports that either.