簡體   English   中英

在C#樹視圖中展開特定節點的所有父節點

[英]Expand all parent nodes of a specific node in C# treeview

我正在C# Treeview創建一個遞歸find方法,並且我想擴展罰款節點的所有父節點。 這是我的代碼:

private void RecursivFindNode(RadTreeNodeCollection nodes, string nodeName2Find)
{
   foreach (RadTreeNode node in nodes)
   {
      if (node.Text.Contains(nodeName2Find))
       {
           node.BackColor = Color.Yellow;
           NodeExpand(node);
       }
       RecursivFindNode(node.Nodes);
    }

}

private void NodeExpand(RadTreeNode nodeExpand)
{
   while (nodeExpand != null)
   {
       nodeExpand.Expand();
       nodeExpand = nodeExpand.Parent;
    }
}

但我收到此錯誤:

Collection was modified; enumeration operation may not execute.

我知道我無法修改foreach loop 那么我該如何運作呢?

因此,正如@Vlad所建議的,我將我的foreach更改為:

foreach (var node in nodes.OfType<RadTreeNode>().ToArray())

現在一切正常。 :)

暫無
暫無

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

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