2011-05-06 94 views
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);     
     } 

如何我得到了项目[1]或项[2]分项?如何在contextmenustrip上的子项上添加事件? C#

回答

1

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]; 
相关问题