[英]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.