如何在上下文菜单条上的子项目上添加事件?C#.
本文关键字:添加 子项目 事件 上下文 菜单 | 更新日期: 2023-09-27 17:55:19
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);
}
如何在项目[1]或项目[2]中获取子项目?
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];