我需要删除没有子节点的父节点,它不是表单的链接,但在处理时总是显示“集合已修改;枚举操作可能无法执行。” 解决这个问题的最佳方法是什么?
Private Sub checkEmptyNode(ByVal T As TreeView)
For Each menuNode As TreeNode In T.Nodes
If menuNode.ChildNodes.Count > 0 Then
For Each childNode As TreeNode In menuNode.ChildNodes
If childNode.ChildNodes.Count > 0 Then
RemoveEmptyNode(childNode.ChildNodes)
Else
If childNode.NavigateUrl.Trim = "" Then
childNode.Parent.ChildNodes.Remove(childNode)
End If
End If
Next
Else
If menuNode.NavigateUrl.Trim = "" Then
T.Nodes.Remove(menuNode)
End If
End If
Next
End Sub
Private Sub RemoveEmptyNode(ByVal TN As TreeNodeCollection)
For Each subChildNode As TreeNode In TN
If subChildNode.ChildNodes.Count > 0 Then
RemoveEmptyNode(subChildNode.ChildNodes)
Else
If subChildNode.NavigateUrl.Trim = "" Then
TN.Remove(subChildNode)
End If
End If
Next
End Sub