2012-07-18 5 views
0

나는 미리 구성된 TreeView 컨트롤을 가지고 있습니다. 데이터베이스에 저장된 값에 따라 권한 집합으로 노드를 제거하고 싶습니다. 재귀 적 메서드를 사용하여 노드를 삭제했지만 일부 노드는 남아 있고 삭제되지 않습니다. 여기 내 코드는 다음과 같습니다재귀 함수를 사용하여 TreeView에서 여러 노드 제거

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 

이 코드는 하나의 부모 노드와 자녀 노드에서 작동하지만, 1 개 이상의 부모 노드가있는 경우 작동하지 않습니다. 부모 노드가 3 개인 경우 해당 부모 노드 중 하나가 삭제되지 않습니다.

아래와 같이 코드를 변경했습니다.

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 

답변

0

그냥이 코드를보고 말을 열심히하지만 나에게 이상한 모양 않는 한 가지가 setNodes(TreeNode) 방법, 당신은 마지막 자식 노드에 자신을 재귀 적으로 호출을 가지고있다. 각 노드에 대해이 작업을 수행하려면 If 구문을 루프 For 루프로 이동해야합니다. 예 :

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 
+0

감사합니다. 노드를 제거한 후 트리 노드 콜렉션이 변경되어 for 루프를 사용하여 코드를 변경 했으므로 적절한 결과를 얻지 못했습니다. 루프 내에서 진술을 언급 했으므로 큰 실수였습니다. 고맙습니다. –

관련 문제