gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:41:21 24 4
gpt4 key购买 nike

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

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

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 处理程序中,只需进行较少的修改。

最佳答案

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

关于delphi - 如何找出 TMenuItem 属于哪个 TPopupMenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6160888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com