Leer en inglés

Compartir a través de


TreeView.AfterCheck Evento

Definición

Se produce después de activarse la casilla del nodo de árbol.

C#
public event System.Windows.Forms.TreeViewEventHandler AfterCheck;
C#
public event System.Windows.Forms.TreeViewEventHandler? AfterCheck;

Tipo de evento

Ejemplos

En el ejemplo de código siguiente se actualizan todos los nodos de árbol secundarios de cuando TreeNode el usuario cambia su estado comprobado. Este código requiere que tenga un Form objeto con un TreeView objeto que tenga TreeNode objetos en su TreeNodeCollection. TreeNodeCollection debe tener nodos de árbol con nodos secundarios.

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

Comentarios

Establecer la TreeNode.Checked propiedad desde dentro de un BeforeCheck controlador de eventos o AfterCheck hace que el evento se genere varias veces y puede dar lugar a un comportamiento inesperado. Para evitar que el evento se genere varias veces, agregue lógica al controlador de eventos que solo ejecute el código recursivo si la Action propiedad de no TreeViewEventArgs está establecida en TreeViewAction.Unknown.

Para obtener más información sobre el manejo de eventos, consulte controlar y provocar eventos.

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, 10

Consulte también