开发者

how to add event on subitem on contextmenustrip? C#

开发者 https://www.devze.com 2023-03-03 21:58 出处:网络
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
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] ?


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消