TechTricks
Technical answers from the trenches 
 
 
 
 

     
   
Displaying Popup Menus from Buttons
 
   
 Posted: 24 February 2004
 
   
 
 Applies to: All versions
 
   
 
Audience: Beginner
 
       
   

Question: I want to display a menu when a user clicks a button. How do I do this?

Answer: There are a number of different ways to do this, but one of the easiest is to:

  1. Assign a popup Menu to the PopupMenu property of a button.

  2. Set the popup menu's AutoPopup property to False.

  3. Add code to the button's OnClick event to position and open the menu, as shown in the following code sample:

    procedure TForm1.Button1Click(Sender: TObject);
    var
       p : TPoint;
    begin
       with Sender as TButton do
       begin
          if PopupMenu = nil then
             beep
          else
             begin
                GetCursorPos( p );
                p.x := Left + 1;
                p.y := Top + Height + 1;
                p := Self.ClientToScreen( p );
                PopupMenu.popup( p.x, p.y );
             end;
       end;
    end;

Use care to typecast Sender correctly; otherwise, you may receive unexpected exceptions. For example, casting Sender as a TSpeedButton when running it from a TButton generates an Invalid Typecast exception when you click the button.

 

       

Top

Feedback About Paradox Delphi Assorted Web Stuff
 
 
Copyright © 2000-2004, techtricks.com; All Rights Reserved.
Acknowledgements, Disclaimers, Terms and Conditions.
Article last updated on 24 February 2004

 

Other Sites: Paradox, Delphi, Perl, Web Stuff, and More


 

[- End -]