[英]Removing multiple nodes in TreeView with recursive function
I have a pre-built TreeView
control. 我有一个预建的
TreeView
控件。 I want to remove the nodes as a permission set according to values that are saved in a database. 我想根据数据库中保存的值将节点作为权限集删除。 I used a recursive method to delete the nodes, but some nodes remain and don't get deleted.
我使用了一种递归方法来删除节点,但是有些节点仍然存在,并且不会被删除。 Here's my code:
这是我的代码:
Private Sub setNodes()
For Each nd As TreeNode In TreeView1.Nodes
If nd.Name = "Students" AndAlso row.Item("CanAddStudents") = False AndAlso row.Item("CanViewStudents") = False AndAlso row.Item("CanImportStudents") = False Then
nd.Remove()
nd.Tag = False
End If
If Not nd.Tag = False Then
setNodes(nd)
End If
nd.Tag = True
Next
End Sub
Private Sub setNodes(ByVal nd As TreeNode)
For Each childNd As TreeNode In nd.Nodes
If childNd.Name = "Registration" AndAlso row.Item("CanAddStudents") = False Then
childNd.Remove()
childNd.Tag = False
ElseIf childNd.Name = "View_Registration" AndAlso row.Item("CanViewStudents") = False Then
childNd.Remove()
childNd.Tag = False
ElseIf childNd.Name = "Import_Student" AndAlso row.Item("CanImportStudents") = False Then
childNd.Remove()
childNd.Tag = False
End if
Next
If Not childNd.Tag = False Then
setNodes(childNd)
End If
childNd.Tag = True
End Sub
This code works on single parent nodes and their child nodes, but it does not work when there is more than 1 parent nodes. 该代码可用于单个父节点及其子节点,但是当父节点超过1个时,此代码将无效。 If there are 3 parents nodes, then one of those parent nodes will not delete.
如果有3个父节点,则这些父节点之一将不会删除。
I changed my code as below. 我如下更改了代码。
Private Sub RemoveNodes(ByVal nc As TreeNodeCollection)
For i As Integer = nc.Count - 1 To 0 Step -1
If nc(i).Nodes.Count > 0 Then
RemoveNodes(nc(i).Nodes)
End If
If nc(i).Name = "Registration" AndAlso row.Item("CanAddStudents") = False Then
nc.RemoveAt(i)
ElseIf nc(i).Name = "View_Registration" AndAlso row.Item("CanViewStudents") = False Then
nc(i).Remove()
ElseIf nc(i).Name = "Import_Student" AndAlso row.Item("CanImportStudents") = False Then
nc(i).Remove()
ElseIf nc(i).Name = "Students" AndAlso row.Item("CanAddStudents") = False AndAlso row.Item("CanViewStudents") = False AndAlso row.Item("CanImportStudents") = False Then
nc(i).Remove()
End If
Next
End Sub
It's hard to say just by looking at this code, but one thing that does look odd to me is that in the setNodes(TreeNode)
method, you only have it calling itself recursively on the last child node. 仅通过看一下这段代码就很难说,但是对我来说确实有些奇怪的是,在
setNodes(TreeNode)
方法中,只让它在最后一个子节点上递归地调用自己。 If you want to do it for each node, you need to move the bottom If
statement up into your For
loop. 如果要对每个节点执行此操作,则需要将底部的
If
语句上移到For
循环中。 For instance: 例如:
For Each childNd As TreeNode In nd.Nodes
If childNd.Name = "Registration" AndAlso row.Item("CanAddStudents") = False Then
childNd.Remove()
childNd.Tag = False
ElseIf childNd.Name = "View_Registration" AndAlso row.Item("CanViewStudents") = False Then
childNd.Remove()
childNd.Tag = False
ElseIf childNd.Name = "Import_Student" AndAlso row.Item("CanImportStudents") = False Then
childNd.Remove()
childNd.Tag = False
End if
'Put recursive call inside loop
If Not childNd.Tag = False Then
setNodes(childNd)
End If
childNd.Tag = True
Next
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.