上下文菜单父? [英] context menu parent?

查看:207
本文介绍了上下文菜单父?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我添加标签上的上下文菜单(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);
        }
    }
}

这篇关于上下文菜单父?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆