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
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
    LVL 36

    Accepted Solution

    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


    Author Comment

    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 36

    Expert Comment

    by:Geert Gruwez
    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

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
    Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now