2012-11-30 2 views
0

노드를 추가하는 재귀 프로그램이 있습니다. treeview를 사용하고 있지만이를 수행하기위한 상향식 접근 방법을 원합니다.하위 노드를 포함하여 노드에서 선택된 ChildNodes의 수를 얻고 싶습니다.

지금까지 이런 짓을했는지하지만

를 작동하지
private void TreeviewCountCheckedNodes(TreeNodeCollection treeNodeCollection) 
{ 
    TreeNode node = treeNodeCollection[0]; 
    int countchecked = 0; 
    if (node != null) 
    { 
     foreach (TreeNode childnode in node.Nodes) 
     { 
      if (childnode.Nodes.Count == 0 && childnode.Checked) 
      { 
       countchecked++; 
      } 
      else if (childnode.Nodes.Count > 0) 
      { 
       TreeviewCountCheckedNodes(childnode.Nodes); 
      } 
     } 
    } 
} 
+2

안녕하세요. 오버플로를 환영합니다. [지금까지 해본 내용을 반영하여 질문을 편집 할 수 있습니까?] (http://mattgemmell.com/2008/12/08/what-have-you-tried/) – kush

+0

프로그래밍 언어를' 태그'는 코드 하이라이트를 얻기 위해 – Simulant

+0

@Simulant 알지 못 했습니까, 깔끔하게. – Rawling

답변

1
private int TreeviewCountCheckedNodes(TreeNodeCollection treeNodeCollection) 
     { 
      int countchecked = 0; 
      if (treeNodeCollection != null) 
      { 
       foreach (TreeNode node in treeNodeCollection) 
       { 
        if (node.Nodes.Count == 0 && node.Checked) 
        { 
         countchecked++; 
        } 
        else if (node.Nodes.Count > 0) 
        { 
         countchecked += TreeviewCountCheckedNodes(node.Nodes); 
        } 
       } 
      } 
      return countchecked; 
     } 

전화 : 내가 누군가를 도울 수 있다는 생각을 여기에이 코드를 게시하고

int coount = TreeviewCountCheckedNodes(treeView.Nodes); 
+0

응답 해 주셔서 감사합니다! 모든 재귀 호출에서 카운트가 0으로 재설정되고 있지만 하위의 카운트도 갖고 싶습니다. –

+0

트리 뷰의 체크 된 모든 리프를 계산합니다. 당신은 결과에 체크 된 서브 노드를 포함하기를 원한다는 것을 의미합니까? – Arie

0

.

코드는 트리보기에서 모든 검사 노드의 수를 검색합니다.

int checkedNodesCount = 0; 

private int GetCheckedNodesCount(TreeNodeCollection treeNodeCollection) 
    { 

     TreeNode node = treeNodeCollection[0]; 


     if (node != null) 
     { 
      if (node.Nodes.Count > 0) 
      { 
       foreach (TreeNode childnode in node.Nodes) 
       { 
        if (childnode.Nodes.Count == 0 && childnode.Checked) 
        { 
         checkedNodesCount++; 
        } 
        else if (childnode.Nodes.Count > 0) 
        { 
         checkedNodesCount += GetCheckedNodesCount(childnode.Nodes); 
        } 
       } 
      } 
      else 
      { 
       if (node.Checked) 
       { 
        checkedNodesCount++; 
       } 
      } 
     } 
     return checkedNodesCount; 
    } 
관련 문제