11 Vote

Delphi/Lazarus: Which Control has called a PopUp Menu? Sender of TPopupMenu?

Question by Guest | 2013-09-22 at 19:47

I am using the same PopUpMenu for various controls (ListView and ListBox) in my project.

So far, calling the pop-up menu works quite well, but there are problems when it is up to determine the Sender/Caller of the menu. So, for example, I want to know from which of the ListViews the PopUp Menu was invoked.

In addition, my first try was to take the variable "Sender" from the procedure "OnPopUp" but strangely enough the Sender is always the same, so I cannot use this to reach my goal.

Another idea was to store in a variable on which control was clicked last. Of course, that will work, but I think that is too much complicated! There must be an easier way to find out from where the PopUp was called, right?

ReplyPositiveNegativeDateVotes
3Best Answer3 Votes

Yes, there is a much easier way. Namely, the property PopupComponent provided by the TPopupMenu itself. In this property, it is always saved, which component has called the PopUp for the last time.

Here's an example:

if PopupMenu1.PopupComponent=ListView1 then begin
   ShowMessage('Called from ListView1');
end;

if PopupMenu1.PopupComponent=ListView2 then begin
   ShowMessage('Called from ListView2');
end;

if PopupMenu1.PopupComponent is TListView then begin
   ShowMessage('Called from a ListView');
end;

In the first examples, we are asking directly whether it has been called by ListView1 or ListView2. The third example generally reacts when the popup is called by a ListView - so it reacts to both, ListView1 as well as ListView2. 
2013-09-24 at 18:41

ReplyPositive Negative
00 Votes

var

   dbGridX : TDBGrid;

begin

   if not (FindComponent(PopupMenu1.PopupComponent.Name) is TDBGrid) then Exit;

   dbGridX := TDBGrid(FindComponent(PopupMenu1.PopupComponent.Name));

   ...

end;
2019-07-28 at 16:53

ReplyPositive Negative
Reply

Related Topics

PHP: Sending an E-Mail

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.