TreeView.BeforeCollapse 이벤트

정의

트리 노드가 축소되기 전에 발생합니다.

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

이벤트 유형

예제

다음 코드 예제에서는 의 축소 상태를 TreeView 변경 하는 방법에 설명 합니다 모든 확인 된 노드가 표시 됩니다. 먼저 모든 노드가 확장되고 이벤트에 대한 BeforeCollapse 처리기가 추가됩니다. 다음으로 모든 노드가 축소됩니다. BeforeCollapse 이벤트 처리기를 지정 된 노드에 검사 되는 자식 노드가 있는지 확인 합니다. 노드에서 자식을 확인한 경우 해당 노드에 대해 축소가 취소됩니다. 노드 옆에 있는 빼기 기호를 클릭 BeforeCollapse 할 때 표준 노드 축소를 허용하기 위해 이벤트 처리기가 제거됩니다.

이 문제를 처리 하 여 구현할 수도 있습니다는 BeforeExpand 이벤트, 해당 항목의 예제에 설명 된 대로 합니다.

전체 예제는 생성자 참조 항목을 참조 TreeView 하세요.

private:
   void showCheckedNodesButton_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      
      // Disable redrawing of treeView1 to prevent flickering 
      // while changes are made.
      treeView1->BeginUpdate();
      
      // Collapse all nodes of treeView1.
      treeView1->ExpandAll();
      
      // Add the checkForCheckedChildren event handler to the BeforeExpand event.
      treeView1->BeforeCollapse += checkForCheckedChildren;
      
      // Expand all nodes of treeView1. Nodes without checked children are 
      // prevented from expanding by the checkForCheckedChildren event handler.
      treeView1->CollapseAll();
      
      // Remove the checkForCheckedChildren event handler from the BeforeExpand 
      // event so manual node expansion will work correctly.
      treeView1->BeforeCollapse -= checkForCheckedChildren;
      
      // Enable redrawing of treeView1.
      treeView1->EndUpdate();
   }

   // Prevent collapse of a node that has checked child nodes.
   void CheckForCheckedChildrenHandler( Object^ /*sender*/, TreeViewCancelEventArgs^ e )
   {
      if ( HasCheckedChildNodes( e->Node ) )
            e->Cancel = true;
   }

   // Returns a value indicating whether the specified 
   // TreeNode has checked child nodes.
   bool HasCheckedChildNodes( TreeNode^ node )
   {
      if ( node->Nodes->Count == 0 )
            return false;

      System::Collections::IEnumerator^ myEnum = node->Nodes->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         TreeNode^ childNode = safe_cast<TreeNode^>(myEnum->Current);
         if ( childNode->Checked )
                  return true;

         // Recursively check the children of the current child node.
         if ( HasCheckedChildNodes( childNode ) )
                  return true;
      }

      return false;
   }
private void showCheckedNodesButton_Click(object sender, EventArgs e)
{
    // Disable redrawing of treeView1 to prevent flickering 
    // while changes are made.
    treeView1.BeginUpdate();

    // Collapse all nodes of treeView1.
    treeView1.ExpandAll();

    // Add the checkForCheckedChildren event handler to the BeforeExpand event.
    treeView1.BeforeCollapse += checkForCheckedChildren;

    // Expand all nodes of treeView1. Nodes without checked children are 
    // prevented from expanding by the checkForCheckedChildren event handler.
    treeView1.CollapseAll();

    // Remove the checkForCheckedChildren event handler from the BeforeExpand 
    // event so manual node expansion will work correctly.
    treeView1.BeforeCollapse -= checkForCheckedChildren;

    // Enable redrawing of treeView1.
    treeView1.EndUpdate();
}

// Prevent collapse of a node that has checked child nodes.
private void CheckForCheckedChildrenHandler(object sender, 
    TreeViewCancelEventArgs e)
{
    if (HasCheckedChildNodes(e.Node)) e.Cancel = true;
}

// Returns a value indicating whether the specified 
// TreeNode has checked child nodes.
private bool HasCheckedChildNodes(TreeNode node)
{
    if (node.Nodes.Count == 0) return false;
    foreach (TreeNode childNode in node.Nodes)
    {
        if (childNode.Checked) return true;
        // Recursively check the children of the current child node.
        if (HasCheckedChildNodes(childNode)) return true;
    }
    return false;
}
Private Sub showCheckedNodesButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    ' Disable redrawing of treeView1 to prevent flickering 
    ' while changes are made.
    treeView1.BeginUpdate()

    ' Collapse all nodes of treeView1.
    treeView1.ExpandAll()

    ' Add the CheckForCheckedChildren event handler to the BeforeExpand event.
    AddHandler treeView1.BeforeCollapse, AddressOf CheckForCheckedChildren

    ' Expand all nodes of treeView1. Nodes without checked children are 
    ' prevented from expanding by the checkForCheckedChildren event handler.
    treeView1.CollapseAll()

    ' Remove the checkForCheckedChildren event handler from the BeforeExpand 
    ' event so manual node expansion will work correctly.
    RemoveHandler treeView1.BeforeCollapse, AddressOf CheckForCheckedChildren
    ' Enable redrawing of treeView1.
    treeView1.EndUpdate()
End Sub


' Prevent collapse of a node that has checked child nodes.
Private Sub CheckForCheckedChildren(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs)
    If HasCheckedChildNodes(e.Node) Then
        e.Cancel = True
    End If
End Sub

' Returns a value indicating whether the specified 
' TreeNode has checked child nodes.
Private Function HasCheckedChildNodes(ByVal node As TreeNode) As Boolean
    If node.Nodes.Count = 0 Then
        Return False
    End If
    Dim childNode As TreeNode
    For Each childNode In node.Nodes
        If childNode.Checked Then
            Return True
        End If
        ' Recursively check the children of the current child node.
        If HasCheckedChildNodes(childNode) Then
            Return True
        End If
    Next childNode
    Return False
End Function 'HasCheckedChildNodes

설명

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

적용 대상

추가 정보