TreeView.BeforeCheck 이벤트

정의

트리 노드 확인란이 선택되기 전에 발생합니다.

public:
 event System::Windows::Forms::TreeViewCancelEventHandler ^ BeforeCheck;
public event System.Windows.Forms.TreeViewCancelEventHandler BeforeCheck;
public event System.Windows.Forms.TreeViewCancelEventHandler? BeforeCheck;
member this.BeforeCheck : System.Windows.Forms.TreeViewCancelEventHandler 
Public Custom Event BeforeCheck As TreeViewCancelEventHandler 

이벤트 유형

예제

다음 코드 예제에서는 사용자가 확인 된 상태를 변경 하는 경우 의 TreeNode 모든 자식 트리 노드를 업데이트 합니다. 이 코드에는 FormTreeView 에 개체가 있는 가 TreeNode 있어야 합니다 TreeNodeCollection. 에 TreeNodeCollection 자식 노드가 있는 트리 노드가 있어야 합니다.

// Updates all child tree nodes recursively.
void CheckAllChildNodes( TreeNode^ treeNode, bool nodeChecked )
{
   IEnumerator^ myEnum = treeNode->Nodes->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      TreeNode^ node = safe_cast<TreeNode^>(myEnum->Current);
      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.
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 );
      }
   }
}
// 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);
      }
   }
}
' Updates all child tree nodes recursively.
Private Sub CheckAllChildNodes(treeNode As TreeNode, nodeChecked As Boolean)
   Dim node As TreeNode
   For Each node In  treeNode.Nodes 
      node.Checked = nodeChecked
      If node.Nodes.Count > 0 Then
         ' If the current node has child nodes, call the CheckAllChildsNodes method recursively.
         Me.CheckAllChildNodes(node, nodeChecked)
      End If
   Next node
End Sub
      
' 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 Sub node_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles treeView1.AfterCheck
   ' The code only executes if the user caused the checked state to change.
   If e.Action <> TreeViewAction.Unknown Then 
      If e.Node.Nodes.Count > 0 Then
         ' Calls the CheckAllChildNodes method, passing in the current 
         ' Checked value of the TreeNode whose checked state changed. 
         Me.CheckAllChildNodes(e.Node, e.Node.Checked)
      End If
   End If
End Sub

설명

참고

TreeNode.Checked 또는 AfterCheck 이벤트 내에서 BeforeCheck 속성을 설정하면 이벤트가 여러 번 발생하고 예기치 않은 동작이 발생할 수 있습니다. 예를 들어 사용자가 개별적으로 확장 및 검사 필요가 없도록 자식 노드를 재귀적으로 업데이트할 때 이벤트 처리기에서 속성을 설정할 Checked 수 있습니다. 이벤트가 여러 번 발생을 방지 하려면 논리만 경우 재귀 코드를 실행 하는 이벤트 처리기를 추가 합니다는 Action 의 속성을 TreeViewEventArgs 로 설정 되지 않은 TreeViewAction.Unknown.

이벤트를 처리 하는 방법에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.

적용 대상

추가 정보