TreeView.CollapseAll TreeView.CollapseAll TreeView.CollapseAll TreeView.CollapseAll Method

Definición

Contrae todos los nodos de árbol.Collapses all the tree nodes.

public:
 void CollapseAll();
public void CollapseAll ();
member this.CollapseAll : unit -> unit
Public Sub CollapseAll ()

Ejemplos

En el ejemplo de código siguiente se muestra cómo cambiar el estado de contracción de un TreeView para que todos los nodos activados están visibles.The following code example demonstrates how to change the collapse state of a TreeView so that all the checked nodes are visible. En primer lugar, se contraen todos los nodos y se agrega un controlador para el BeforeExpand eventos.First, all the nodes are collapsed, and a handler is added for the BeforeExpand event. A continuación, todos los nodos se expanden.Next, all the nodes are expanded. El BeforeExpand controlador de eventos determina si un determinado nodo tiene nodos secundarios que están activados.The BeforeExpand event handler determines whether a given node has child nodes that are checked. Si un nodo no tiene nodos secundarios activados, se cancela la expansión para ese nodo.If a node does not have checked children, the expansion is canceled for that node. Con el fin de permitir la expansión de nodo estándar cuando se hace clic en el signo más junto a un nodo, el BeforeExpand , a continuación, se quita el controlador de eventos.In order to allow standard node expansion when the plus sign next to a node is clicked, the BeforeExpand event handler is then removed.

Este comportamiento también se puede implementar controlando el BeforeCollapse eventos, como se muestra en el ejemplo de ese tema.This behavior can also be implemented by handling the BeforeCollapse event, as illustrated in the example for that topic.

Para obtener un ejemplo completo, vea el CheckBoxes tema de referencia.For the complete example, see the CheckBoxes reference topic.

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->CollapseAll();
      
      // Add the checkForCheckedChildren event handler to the BeforeExpand event.
      treeView1->BeforeExpand += checkForCheckedChildren;
      
      // Expand all nodes of treeView1. Nodes without checked children are 
      // prevented from expanding by the checkForCheckedChildren event handler.
      treeView1->ExpandAll();
      
      // Remove the checkForCheckedChildren event handler from the BeforeExpand 
      // event so manual node expansion will work correctly.
      treeView1->BeforeExpand -= checkForCheckedChildren;
      
      // Enable redrawing of treeView1.
      treeView1->EndUpdate();
   }

   // Prevent expansion of a node that does not have any 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.CollapseAll();

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

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

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

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

// Prevent expansion of a node that does not have any 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.CollapseAll()

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

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

    ' Remove the checkForCheckedChildren event handler from the BeforeExpand 
    ' event so manual node expansion will work correctly.
    RemoveHandler treeView1.BeforeExpand, AddressOf CheckForCheckedChildren

    ' Enable redrawing of treeView1.
    treeView1.EndUpdate()
End Sub 'showCheckedNodesButton_Click

' Prevent expansion of a node that does not have any checked child nodes.
Private Sub CheckForCheckedChildren(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs)
    If Not HasCheckedChildNodes(e.Node) Then
        e.Cancel = True
    End If
End Sub 'CheckForCheckedChildren

' 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

Comentarios

El CollapseAll método contrae todos los TreeNode objetos, que incluye todos los nodos de árbol de secundarios, que se encuentran en el TreeView control.The CollapseAll method collapses all the TreeNode objects, which includes all the child tree nodes, that are in the TreeView control.

Nota

El estado de un TreeNode persiste.The state of a TreeNode persists. Por ejemplo, supongamos que se llama el Expand método para un nodo de árbol raíz.For example, suppose that you call the Expand method for a root tree node. Si los nodos de árbol secundarios no se han contraído previamente, aparecerán en su estado previamente expandidos.If the child tree nodes were not previously collapsed, they will appear in their previously-expanded state. Una llamada a la CollapseAll método garantiza que todos los nodos de árbol aparecen en el estado contraído.Calling the CollapseAll method ensures that all the tree nodes appear in the collapsed state.

Se aplica a

Consulte también: