TreeView.AfterCheck 事件

定义

在选中树节点复选框后发生。

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

事件类型

示例

下面的代码示例在用户更改其选中状态时更新 的所有子树节点 TreeNode 。 此代码要求具有 Form 的 ,TreeViewTreeNodeCollection中具有 TreeNode 对象。 应 TreeNodeCollection 具有具有子节点的树节点。

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);
      }
   }
}

注解

TreeNode.Checked从 或 AfterCheck 事件处理程序中BeforeCheck设置 属性会导致多次引发事件,并可能导致意外行为。 若要防止多次引发事件,请将逻辑添加到事件处理程序,该处理程序仅在 的 属性TreeViewEventArgs未设置为 TreeViewAction.UnknownAction执行递归代码。

有关处理事件的详细信息,请参阅 处理和引发事件

适用于

产品 版本
.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

另请参阅