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

Hello,

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.

Thanks,

Ben
AivsCoderAsked:
Who is Participating?
 
Geert GOracle dbaCommented:
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);
begin
  ShowMessage(Sender.ClassName);
end;

this will give the type of class of the sender

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


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

0
 
AivsCoderAuthor Commented:
thanks
0
 
AivsCoderAuthor Commented:
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;
begin
vtSender := TVirtualStringTree(sender).Name; inside the OnClickEvent for VirtualStringTree
end;

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??
Ben

0
 
Geert GOracle dbaCommented:
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

type
  TFormX = class(TForm)
  private
    fLastFocusedVTS: TVirtualStringTree;
  end;

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

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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.