delphi - 如何找出 TMenuItem 属于哪个 TPopupMenu
应该很简单,但我看不到。
您可以通过以下方式找到右键单击以显示弹出菜单的组件:
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 ;
它还询问涉及哪个 TMenuItem,因此给了我一段代码,我可以将其放入其他 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对你的问题有点困惑,但由于你已经排除了其他所有内容,我只能想象你正在寻找
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
.