英語で読む

次の方法で共有


TreeView.AfterCheck イベント

定義

ツリー ノードのチェック ボックスがオンにされた後に発生します。

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

イベントの種類

次のコード例では、ユーザーがチェック状態を変更したときに、 TreeNode のすべての子ツリー ノードを更新します。 このコードでは、 内に オブジェクトを持Formつ を持TreeNodeTreeView が必要TreeNodeCollectionです。 には 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が に設定されていない場合Actionにのみ再帰コードを実行するロジックをイベント ハンドラーにTreeViewAction.Unknown追加します。

イベントの処理の詳細については、「処理とイベントの発生」を参照してください。

適用対象

製品 バージョン
.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

こちらもご覧ください