如何在上下文菜单条上的子项上添加事件? C#

发布于 2024-11-05 18:14:42 字数 388 浏览 0 评论 0原文

for (int i = 0; i < client.Folders.Count; i++)
        {

            (ContextMenuListView.Items[1] as ToolStripMenuItem).DropDownItems.Add(client.Folders[i].Name);//add Folder to Move To
            (ContextMenuListView.Items[2] as ToolStripMenuItem).DropDownItems.Add(client.Folders[i].Name);                
        }

如何获取 Items[1] 或 Items[2] 中的子项目?

for (int i = 0; i < client.Folders.Count; i++)
        {

            (ContextMenuListView.Items[1] as ToolStripMenuItem).DropDownItems.Add(client.Folders[i].Name);//add Folder to Move To
            (ContextMenuListView.Items[2] as ToolStripMenuItem).DropDownItems.Add(client.Folders[i].Name);                
        }

how to I get subitem in Items[1] or Items[2] ?

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

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

发布评论

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

评论(1

晨曦÷微暖 2024-11-12 18:14:42

ToolStripItemCollection.Add(string) (DropDownItems.Add()) 将返回新的 ToolStripItem ...

另一方面,所有其他子项均由 ToolStripItemCollection DropDownItems 引用

因此,获取两个创建的项目的简单方法是:

(ContextMenuListView.Items[1] as ToolStripMenuItem).DropDownItems.Add(client.Folders[i].Name);
(ContextMenuListView.Items[2] as ToolStripMenuItem).DropDownItems.Add(client.Folders[i].Name);

将成为:

ToolStripItem firstItem = (ContextMenuListView.Items[1] as ToolStripMenuItem).DropDownItems.Add(client.Folders[i].Name);
ToolStripItem secondItem = (ContextMenuListView.Items[2] as ToolStripMenuItem).DropDownItems.Add(client.Folders[i].Name);

或访问所有子项目:

foreach(ToolStripItem i in (ContextMenuListView.Items[1] as ToolStripMenuItem).DropDownItems.OfType<ToolStripItem>())
{
   //...
}

或访问特定子项目:

var specificItem = (ContextMenuListView.Items[1] as ToolStripMenuItem).DropDownItems.Item[0];

ToolStripItemCollection.Add(string) (DropDownItems.Add()) will return the new ToolStripItem ...

on the other hand, all other sub items are referenced by the ToolStripItemCollection DropDownItems

so the easy way to get the two created items is:

(ContextMenuListView.Items[1] as ToolStripMenuItem).DropDownItems.Add(client.Folders[i].Name);
(ContextMenuListView.Items[2] as ToolStripMenuItem).DropDownItems.Add(client.Folders[i].Name);

would become:

ToolStripItem firstItem = (ContextMenuListView.Items[1] as ToolStripMenuItem).DropDownItems.Add(client.Folders[i].Name);
ToolStripItem secondItem = (ContextMenuListView.Items[2] as ToolStripMenuItem).DropDownItems.Add(client.Folders[i].Name);

or to access all sub items:

foreach(ToolStripItem i in (ContextMenuListView.Items[1] as ToolStripMenuItem).DropDownItems.OfType<ToolStripItem>())
{
   //...
}

or to access a specific sub item:

var specificItem = (ContextMenuListView.Items[1] as ToolStripMenuItem).DropDownItems.Item[0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文