TreeView.BeforeCollapse Evento

Definición

Se produce antes de contraerse el nodo de árbol.

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 

Tipo de evento

Ejemplos

En el ejemplo de código siguiente se muestra cómo cambiar el estado de contracción de para TreeView que todos los nodos comprobados estén visibles. En primer lugar, se expanden todos los nodos y se agrega un controlador para el BeforeCollapse evento . A continuación, se contraen todos los nodos. El BeforeCollapse controlador de eventos determina si un nodo determinado tiene nodos secundarios que se comprueban. Si un nodo ha comprobado los elementos secundarios, el contraer se cancela para ese nodo. Para permitir que el nodo estándar se contraiga cuando se hace clic en el signo menos situado junto a un nodo, se quita el BeforeCollapse controlador de eventos.

Este comportamiento también se puede implementar controlando el BeforeExpand evento, como se muestra en el ejemplo de ese tema.

Para obtener el ejemplo completo, consulte el tema de referencia del TreeView constructor.

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

Comentarios

Para obtener más información acerca de cómo controlar eventos, vea controlar y provocar eventos.

Se aplica a

Consulte también