簡體   English   中英

如何在contextmenustrip的子項目上添加事件? C#

[英]how to add event on subitem on contextmenustrip? C#

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]中的子項目?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM