上下文菜单父?

编程入门 行业动态 更新时间:2024-10-26 20:21:46
本文介绍了上下文菜单父?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好我添加标签上的上下文菜单(C#的WinForms)。具有3个孩子的项目和我的上下文菜单我想显示当我点击上下文菜单项中的任何一个标签文本。

Hi I added a context menu on label (c#, winforms). my context menu having 3 child items and i want to display label text when i click on any one of context menu items.

在此先感谢

推荐答案

的 的ContextMenuStrip 控件具有的 SourceControl 财产,将必须打开它的控制提供参考。你可以用它来提取控件中的文本:

The ContextMenuStrip control has a SourceControl property, that will have a reference to the control that opened it. You can use that to extract the text from the control:

private void MenuStripItem_Click(object sender, EventArgs e) { ToolStripItem item = (sender as ToolStripItem); if (item != null) { ContextMenuStrip owner = item.Owner as ContextMenuStrip; if (owner != null) { MessageBox.Show(owner.SourceControl.Text); } } }

。如果你不是一个的ContextMenuStrip 使用文本菜单,代码应该是这样的:

If you instead of a ContextMenuStrip use a ContextMenu, the code should look like this:

private void menuItem1_Click(object sender, EventArgs e) { MenuItem item = (sender as MenuItem); if (item != null) { ContextMenu owner = item.Parent as ContextMenu; if (owner != null) { MessageBox.Show(owner.SourceControl.Text); } } }

更多推荐

上下文菜单父?

本文发布于:2023-11-23 02:19:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619725.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   菜单

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!