0
我在C#中創建遞歸查找方法Treeview
,並且我想擴展精細節點的所有父節點。這是我的代碼:在C#treeview中展開特定節點的所有父節點
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
的項目。那麼我怎麼能使它工作?
爲什麼一個方法,按名稱應該只是「找到」節點(或者說是一個*節點?),也擴大它們/它呢?讓它只返回找到的節點。然後有第三種方法,稱爲「查找」,之後調用「擴展」。此外,將搜索文本作爲輸入參數可能會更好,而不是從隱蔽文本框中檢索它(我認爲)。最後,如果你仍然得到這個錯誤,你可能想要在foreach中實現第二個集合(例如使用Linq的'nodes.ToList()'),看看是否有幫助。 – Corak 2014-11-24 07:29:57
嘗試'foreach(var節點中的節點.OfType().ToArray()' –
Vlad
2014-11-24 07:49:49
@Vlad謝謝!這是我的解決方案! – 2014-11-24 09:03:07