如何返回 .Net 中 ContextMenuStrip 中子菜单内 ToolStripMenuItem 的名称

发布于 2024-11-01 14:53:48 字数 651 浏览 4 评论 0原文

我的问题可能含糊不清,但这是我的情况:

我的表单上有一个方形的图片框数组,每个图片框都有一个处理程序来打开 ContextMenuStrip,其内容是根据目录生成的。目录中的每个文件夹将创建一个 ToolStripMenuItem,并且该文件夹内的每个文件将在所述菜单菜单项的 DropDownItems 内表示。单击菜单的子项后,图片框的图像将根据单击的菜单项而变化。

当我尝试找出单击了哪个子项目时,出现了我的问题。如何通过 ContextMenuStrip 的 _Clicked 事件找到它?到目前为止,这是我的尝试:

        private void mstChooseTile_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        ContextMenuStrip s = (ContextMenuStrip)sender;
        ToolStripMenuItem x = (ToolStripMenuItem)e.ClickedItem;
        // Test to see if I can get the name
        MessageBox.Show(x.DropDownItems[1].Name);
        // Nope :(
    }

My question might be ambiguous but here is my situation :

I have a square array of pictureboxes on my form, each has a handler to open the ContextMenuStrip whose content is generated based on a directory. Each folder in the directory will create a ToolStripMenuItem and each files inside that folder will be represented inside the DropDownItems of said menu menu item. Upon clicking on a sub item of my menu the picturebox's image will change based on what menu item was clicked.

My problem arises when I try to find out which sub item was clicked. How can I find that out with the _Clicked event of the ContextMenuStrip ? Here is my attempt so far :

        private void mstChooseTile_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        ContextMenuStrip s = (ContextMenuStrip)sender;
        ToolStripMenuItem x = (ToolStripMenuItem)e.ClickedItem;
        // Test to see if I can get the name
        MessageBox.Show(x.DropDownItems[1].Name);
        // Nope :(
    }

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

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

发布评论

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

评论(1

只是我以为 2024-11-08 14:53:48

ItemClicked 事件对您不起作用:

A) 它仅对直接子级起作用。

B) 即使单击非叶节点也会触发。

尝试订阅每个 ToolStripMenuItem。这里我跳过订阅非叶子节点。

using System;
using System.Windows.Forms;

public class Form1 : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    public Form1()
    {
        ContextMenuStrip = new ContextMenuStrip
        {
            Items =
            {
                new ToolStripMenuItem
                {
                    Text = "One",
                    DropDownItems =
                    {
                        new ToolStripMenuItem { Text = "One.1" },
                        new ToolStripMenuItem { Text = "One.2" },
                        new ToolStripMenuItem { Text = "One.3" },
                        new ToolStripMenuItem { Text = "One.4" },
                    },
                },
                new ToolStripMenuItem
                {
                    Text = "Two",
                },
                new ToolStripMenuItem
                {
                    Text = "Three",
                    DropDownItems =
                    {
                        new ToolStripMenuItem { Text = "Three.1" },
                        new ToolStripMenuItem { Text = "Three.2" },
                    },
                },
            }
        };

        foreach (ToolStripMenuItem item in ContextMenuStrip.Items)
            Subscribe(item, ContextMenu_Click);
    }

    private static void Subscribe(ToolStripMenuItem item, EventHandler eventHandler)
    {
        // If leaf, add click handler
        if (item.DropDownItems.Count == 0)
            item.Click += eventHandler;
        // Otherwise recursively subscribe
        else foreach (ToolStripMenuItem subItem in item.DropDownItems)
            Subscribe(subItem, eventHandler);
    }

    void ContextMenu_Click(object sender, EventArgs e)
    {
        MessageBox.Show((sender as ToolStripMenuItem).Text, "The button clicked is:");
    }
}

The ItemClicked event isn't going to work for you:

A) It only works for immediate children.

B) It fires even when clicking non-leaf nodes.

Try subscribing to each ToolStripMenuItem instead. Here I skip subscribing to non-leaf nodes.

using System;
using System.Windows.Forms;

public class Form1 : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    public Form1()
    {
        ContextMenuStrip = new ContextMenuStrip
        {
            Items =
            {
                new ToolStripMenuItem
                {
                    Text = "One",
                    DropDownItems =
                    {
                        new ToolStripMenuItem { Text = "One.1" },
                        new ToolStripMenuItem { Text = "One.2" },
                        new ToolStripMenuItem { Text = "One.3" },
                        new ToolStripMenuItem { Text = "One.4" },
                    },
                },
                new ToolStripMenuItem
                {
                    Text = "Two",
                },
                new ToolStripMenuItem
                {
                    Text = "Three",
                    DropDownItems =
                    {
                        new ToolStripMenuItem { Text = "Three.1" },
                        new ToolStripMenuItem { Text = "Three.2" },
                    },
                },
            }
        };

        foreach (ToolStripMenuItem item in ContextMenuStrip.Items)
            Subscribe(item, ContextMenu_Click);
    }

    private static void Subscribe(ToolStripMenuItem item, EventHandler eventHandler)
    {
        // If leaf, add click handler
        if (item.DropDownItems.Count == 0)
            item.Click += eventHandler;
        // Otherwise recursively subscribe
        else foreach (ToolStripMenuItem subItem in item.DropDownItems)
            Subscribe(subItem, eventHandler);
    }

    void ContextMenu_Click(object sender, EventArgs e)
    {
        MessageBox.Show((sender as ToolStripMenuItem).Text, "The button clicked is:");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文