Thursday 9 August 2007

Pop-up menu from button

Recently I had to create a pop-up menu when pressing a button, as shown on the picture.

Here is the code for this task to complete in Borland Delphi.
case PopupMenu1.Items.Count of
0 : // no menu items
begin
MessageDlg( _( 'No items to show' ), mtInformation, [mbOK], 0 );
exit;
end;
1 : // only one item - execute
PopupMenu1.Items[0].Click;
else
with BitBtn1, ClientToScreen( Point( 0, Height ) ) do
PopupMenu1.Popup( X, Y );
end;
BitBtn1 is the button to be pressed, PopupMenu1 contains the menu items to pop up. If there's no menu items, a message is shown, if only one menu item, it is launched by default, if more - popup menu appears.

No comments: