delphi - 如何找出 TMenuItem 属于哪个 TPopupMenu

发布于 2024-11-10 09:04:16 字数 1013 浏览 4 评论 0原文

应该很简单,但我看不到。

您可以通过以下方式找到右键单击以显示弹出菜单的组件:

PopupMenu1.PopupComponent

但是如何找到包含依次单击该菜单的 TMenuItem 的弹出菜单呢?

为了将问题简化为一个示例:

我有一系列标签,每个标签都有不同的标题,并且有一个分配给每个标签的 PopupMenu 属性的弹出菜单。

当有人右键单击其中一个标签并调出弹出菜单,然后单击 MenuItem1 时,我想编码:

procedure TForm1.MenuItem1Click(Sender: TObject);

begin
MsgBox (Format ('The label right-clicked has the caption %', [xxxx.Caption ])) ;
end ;

xxxx 应该是什么?

已实施的答案

感谢两位受访者。我最终得到的是这样的:

procedure TForm1.MenuItem1Click(Sender: TObject);

var
    AParentMenu : TMenu ;
    AComponent  : TComponent ;
    ALabel      : TLabel ;

begin
AParentMenu := TMenuItem (Sender).GetParentMenu ;
AComponent  := TPopupMenu (AParentMenu).PopupComponent ;
ALabel      := TLabel (AComponent) ;
MsgBox (Format ('The label right-clicked has the caption %', [ALabel.Caption ])) ;
end ;

它还询问涉及哪个 T​​MenuItem,因此给了我一段代码,我可以将其放入其他 OnClick 处理程序中,只需较少的修改。

Should be simple enough but I can't see it.

You can find out the component that was right-clicked on to display a popup menu with:

PopupMenu1.PopupComponent

but how do you find out the popup menu that contains the TMenuItem that was in turn clicked on that menu?

To simplify the problem to an example:

I have a series of labels, each with a different caption, and I have a popup menu that is assigned to the PopupMenu property of each of the labels.

When someone right-clicks one of the labels and brings up the popup menu, and then clicks on MenuItem1, I want to code:

procedure TForm1.MenuItem1Click(Sender: TObject);

begin
MsgBox (Format ('The label right-clicked has the caption %', [xxxx.Caption ])) ;
end ;

What should xxxx be?

Implemented Answer

Thanks to both respondents. What I ended up with was this:

procedure TForm1.MenuItem1Click(Sender: TObject);

var
    AParentMenu : TMenu ;
    AComponent  : TComponent ;
    ALabel      : TLabel ;

begin
AParentMenu := TMenuItem (Sender).GetParentMenu ;
AComponent  := TPopupMenu (AParentMenu).PopupComponent ;
ALabel      := TLabel (AComponent) ;
MsgBox (Format ('The label right-clicked has the caption %', [ALabel.Caption ])) ;
end ;

which also interrogates which TMenuItem was involved and therefore gives me a fragment of code I can drop into other OnClick handlers with less modification.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唱一曲作罢 2024-11-17 09:04:16

我对你的问题有点困惑,但由于你已经排除了其他所有内容,我只能想象你正在寻找 TMenuItem.GetParentMenu

I'm a little confused by your question but since you've ruled out everything else I can only imagine you are looking for TMenuItem.GetParentMenu.

爱已欠费 2024-11-17 09:04:16
procedure TForm1.MenuItem1Click(Sender: TObject);
var pop:TPopupMenu;
    lbl:TLabel;
begin
  // Firstly get parent TPopupMenu (needs casting from TMenu) 
  pop:= TPopupMenu(MenuItem1.GetParentMenu()); 
  // pop.PopupComponent is the "source" control, just cast it to Tlabel
  lbl:= TLabel(pop.PopupComponent);            

  ShowMessage(Format('The label right-clicked has the caption %s',[lbl.Caption]));
end;
procedure TForm1.MenuItem1Click(Sender: TObject);
var pop:TPopupMenu;
    lbl:TLabel;
begin
  // Firstly get parent TPopupMenu (needs casting from TMenu) 
  pop:= TPopupMenu(MenuItem1.GetParentMenu()); 
  // pop.PopupComponent is the "source" control, just cast it to Tlabel
  lbl:= TLabel(pop.PopupComponent);            

  ShowMessage(Format('The label right-clicked has the caption %s',[lbl.Caption]));
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文