TreeViewCancelEventArgs.Node Propiedad

Definición

Obtiene el nodo de árbol que se va a comprobar, expandir, contraer o seleccionar.

C#
public System.Windows.Forms.TreeNode Node { get; }
C#
public System.Windows.Forms.TreeNode? Node { get; }

Valor de propiedad

TreeNode que se va a comprobar, expandir, contraer o seleccionar.

Ejemplos

En el ejemplo 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, todos los nodos se contraen y se agrega un controlador al TreeView.BeforeExpand evento. A continuación, se expanden todos los nodos. El TreeView.BeforeExpand controlador de eventos determina si un nodo determinado tiene nodos secundarios que se comprueban. Si un nodo no tiene elementos secundarios comprobados, la expansión se cancela para ese nodo. Para permitir la expansión normal del nodo cuando se hace clic en el signo más junto a un nodo, se quita el TreeView.BeforeExpand controlador de eventos.

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

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

C#
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;
}

Se aplica a

Producto Versiones
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Consulte también