How do I make a different components on my form share the same popupmenu and distinguish between the different senders

Posted on 2011-05-06
Medium Priority
Last Modified: 2012-05-11

I have three components on the form which will have the same popupmenu items. To me,  It makes sense for me to use one popupmenu for all three components  . How do I find out which component it is currently doing businesss with? (sender?? or owner??) im not sure.


Question by:AivsCoder
  • 2
  • 2
LVL 38

Accepted Solution

Geert Gruwez earned 500 total points
ID: 35710253
each has a different name
you can find the name by casting the sender

a little trick to find the type of the sender:

procedure TForm1.menuFileOpenClick(Sender: TObject);

this will give the type of class of the sender

next cast it:
assuming you get TMenuItem
procedure TForm1.menuFileOpenClick(Sender: TObject);
  ShowMessage(TMenuItem(Sender).Caption + ' Clicked');

you can also give each menuitem a number in the Tag or give it a specific name
by casting the sender you have access to all it's properties


Author Closing Comment

ID: 35710652

Author Comment

ID: 35710778
Hey I was wondering if you knew the answer to this one:
Its a similar question

I have a popupmenu on a VirtualStringTree and I click on the VirtualStringTree item and then right click on the popup menu and click on the menu item.

The sender is then the menu item and not the VirtualStringTree.
In this example, I need the VirtualStringTree to be the sender and not the PopupMenu.

so I did this:

vtSender : string;
vtSender := TVirtualStringTree(sender).Name; inside the OnClickEvent for VirtualStringTree

but it in order for me to pass vtSender to the procedure that requires it, I have to make it a global variable.

Is there a way to get the sender before the current sender??

LVL 38

Expert Comment

by:Geert Gruwez
ID: 35711965
the popupmenu initiates the onclick event and is the sender of that event

If you have multiple virtualstringtree (or components of the same type)
then you would use the onEnter event on all components of the same type
and store the last focused in a private variable

  TFormX = class(TForm)
    fLastFocusedVTS: TVirtualStringTree;

procedure TFormX.VirtualStringTreeYEnter(Sender: TObject);
  fLastFocusedVTS := TVirtualStringTree(Sender);  

attach this onEnter event to all stringtree's
and in the menuitem event you can check which was the last focused stringtree in fLastFocusedVTS
this is the easiest way

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question