views:

1655

answers:

4

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

+4  A: 

Sure. Put a toolbar on the page. Right-click on the toolbar, add a button. Set the button's style to tbsDropDown. Put a PopupMenu on the page. Double click on the PopupMenu to define menu items. Then, go back to the button you created and set its DropdownMenu property to point to the PopupMenu you just created.

Kluge
+1  A: 

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

RichardS
+5  A: 

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.

Jim McKeeth
The real problem exists if you want this TButton to be a TSpeedButton that stays Down while the menu is displayed and pops up when user clicks an item from the menu OR anywhere else in the form. But that's a different question and a different topic ...
gabr
Couldn't you just put a timer in the speedbutton's onclick event, and have the timer's execute event test to see whether the popupmenu is still visible?
Argalatyr
If the button is placed near the bottom of the screen, the menu will laid out bottom up, starting from the bottom of the button, thus covering it, which might not be what you want.(Probably no problem here, I just thought I'd mention it.)
dummzeuch
+2  A: 

Jim's answer is great, but didn't quite work for me at first. ClientToScreen uses Form86's method, which is only correct if the button is directly on the form. It should be the button's ClientToScreen method that is called, like this:

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.

Chris