1

I have 2 memo's on a form and 1 Tpopupmenu I have assigned the popup menu as the popup menu for both memos. The pop up menu has a couple of menuitem assigned to it, when I right mouse click on either of the memo's and click on one of the menu items on its onclick event I would like to be able to know which memo is the one where the menu was invoked from but cannot figure out how to find out which memo it was!!

Could anyone give me a pointer on how to detect the underlying memo.

thanks

colin

colin
  • 2,983
  • 6
  • 41
  • 49

1 Answers1

6

You can use the PopupComponent property of the popup menu:

procedure TForm1.PopupItemClick(Sender: TObject);
begin
  if PopupMenu1.PopupComponent = Memo1 then
    ..
  else
    ..
Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
  • 1
    Or type-cast the `PopupComponent` value to a `TMemo` pointer and use it as needed. – Remy Lebeau May 10 '12 at 01:02
  • @RemyLebeau: yes, when preceeded with an `is TMemo` check, I'd much prefer a cast (+ assign to a local var) over an if statement. Unless the handling for each memo should be different, but in that case two menu items are probably warrented anyway. – Marjan Venema May 10 '12 at 06:13
  • Thanks, i did think it was PopupComponent but I was using the items PopupComponent which of course was givig me the popup menu. – colin May 10 '12 at 07:40
  • @MarjanVenema: if you know for sure ahead of time that the `TPopupMenu` is only assigned to `TMemo` components, then the `is` check is not needed. – Remy Lebeau May 10 '12 at 15:52
  • @RemyLebeau: ah yes, but things change... and then someone comes along that doesn't know about this restriction and uses the handler for another control... and then boom, or worse, not boom, but a more subtle effect that isn't noticed immediately... and then a couple of ... later, a bug is reported and someone needs to spend hours trying to figure out where it is coming from... When you are treating a typeX var as typeY, I prefer the safety net of an almost immediate runtime slap on the hand of the `is` check :-) – Marjan Venema May 10 '12 at 17:53
  • @RemyLebeau: Admittedly, in this case it may seem a remote possibility, but I like habits that help me prevent errors or help me catch them as soon as possible. – Marjan Venema May 10 '12 at 17:54
  • Personally, I prefer to write code that is not dependant on any particular data type when possible. Depending on what the event handler is actually doing, it could just type-cast the `PopupComponent` to `TControl` or even `TComponent`, either of which would work for any possible component the menu can be assigned to. – Remy Lebeau May 10 '12 at 18:10