Re: which control invoked the popup menu

Posted by Bram on September 19, 2007

In Reply to which control invoked the popup menu posted by delphidauphin on September 15, 2007

: I have a generic popupmenu on a form that can be called by button1, button2, edit99, etc, and what I need to do is find out at runtime what control initiated the call to the popup menu.

: Thanks

There is no way for asking the Delphi PopupMenu how it was "triggered". In other words, there is no property nor method of the TPopupMenu component that gives you a reference to the component that was right-clicked.

I think that the only way to know which button "did it", is: store some value in a unit-wide variable, in the OnMouseDown event of each button. For example, store the button's name, or store its Tag property that you have set differently for each button. You don't have to write 99 event handlers for this, just write one and then set the OnMouseDown event handler of the other buttons to the same procedure.

Example:

var
  ButtonTag: integer;
   
// This event handler is common to Button1 through Button99 
procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbRight then
    ButtonTag := (Sender as TButton).Tag;
end;
 
procedure TForm1.PopupMenu1Popup(Sender: TObject);
begin
  ShowMessage('Button clicked: ' + IntToStr(ButtonTag));
end; 

Succes!
Bram

Follow Ups