반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

타닥타닥 민타쿠

C# 윈폼(Winform) 자식(하위) 노드까지 한번에 체크하기 본문

개발/Winform

C# 윈폼(Winform) 자식(하위) 노드까지 한번에 체크하기

민타쿠 2022. 7. 23. 10:01
반응형

트리뷰의 자식 노드까지 전체 체크, 전체 체크해제 기능은 AfterCheck 이벤트를 통해 간단히 만들 수 있는데,
친절하게도 공식 문서의 TreeView.AfterCheck Event 페이지에 자식 노드의 체크 기능까지 같이 나와있다.

코드는 아래와 같다.

// Updates all child tree nodes recursively.
private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
{
   foreach(TreeNode node in treeNode.Nodes)
   {
      node.Checked = nodeChecked;
      if(node.Nodes.Count > 0)
      {
         // If the current node has child nodes, call the CheckAllChildsNodes method recursively.
         this.CheckAllChildNodes(node, nodeChecked);
      }
   }
}

// NOTE   This code can be added to the BeforeCheck event handler instead of the AfterCheck event.
// After a tree node's Checked property is changed, all its child nodes are updated to the same value.
private void node_AfterCheck(object sender, TreeViewEventArgs e)
{
   // The code only executes if the user caused the checked state to change.
   if(e.Action != TreeViewAction.Unknown)
   {
      if(e.Node.Nodes.Count > 0)
      {
         /* Calls the CheckAllChildNodes method, passing in the current 
         Checked value of the TreeNode whose checked state changed. */
         this.CheckAllChildNodes(e.Node, e.Node.Checked);
      }
   }
}

 

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.treeview.aftercheck?redirectedfrom=MSDN&view=windowsdesktop-6.0 

 

TreeView.AfterCheck Event (System.Windows.Forms)

Occurs after the tree node check box is checked.

docs.microsoft.com

 

반응형
Comments