TreeView.BeforeCheck イベント
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As TreeView Dim handler As TreeViewCancelEventHandler AddHandler instance.BeforeCheck, handler
public: event TreeViewCancelEventHandler^ BeforeCheck { void add (TreeViewCancelEventHandler^ value); void remove (TreeViewCancelEventHandler^ value); }

![]() |
---|
TreeNode.Checked プロパティを BeforeCheck イベント内または AfterCheck イベント内から設定すると、イベントが複数回発生することになり、予期できない動作を引き起こす可能性があります。たとえば、子ノードを再帰的に更新するときは、イベント ハンドラで Checked プロパティを設定することによって、ユーザーは各ノードを展開してチェックする必要がなくなります。イベントが複数回発生することを防ぐためには、TreeViewEventArgs の Action プロパティが TreeViewAction.Unknown に設定されていない場合に、再帰コードだけを実行するロジックをイベント ハンドラに追加します。 |

ユーザーがチェック状態を変更したときに TreeNode のすべての子ツリー ノードを更新するコード例を次に示します。このコードは、TreeNode オブジェクトの TreeNodeCollection を持つ TreeView が、Form に配置されていることを前提としています。TreeNodeCollection は、子ノードを持つツリー ノードを持っている必要があります。
' Updates all child tree nodes recursively. Private Sub CheckAllChildNodes(treeNode As TreeNode, nodeChecked As Boolean) Dim node As TreeNode For Each node In treeNode.Nodes node.Checked = nodeChecked If node.Nodes.Count > 0 Then ' If the current node has child nodes, call the CheckAllChildsNodes method recursively. Me.CheckAllChildNodes(node, nodeChecked) End If Next node End Sub ' 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 Sub node_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles treeView1.AfterCheck ' The code only executes if the user caused the checked state to change. If e.Action <> TreeViewAction.Unknown Then If e.Node.Nodes.Count > 0 Then ' Calls the CheckAllChildNodes method, passing in the current ' Checked value of the TreeNode whose checked state changed. Me.CheckAllChildNodes(e.Node, e.Node.Checked) End If End If End Sub
// 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); } } }
// Updates all child tree nodes recursively. void CheckAllChildNodes( TreeNode^ treeNode, bool nodeChecked ) { IEnumerator^ myEnum = treeNode->Nodes->GetEnumerator(); while ( myEnum->MoveNext() ) { TreeNode^ node = safe_cast<TreeNode^>(myEnum->Current); 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. 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 ); } } }
// Updates all child tree nodes recursively. private void CheckAllChildNodes(TreeNode treeNode, boolean nodeChecked) { for (int iCtr = 0; iCtr < treeNode.get_Nodes().get_Count(); iCtr++) { TreeNode node = treeNode.get_Nodes().get_Item(iCtr); node.set_Checked(nodeChecked); if (node.get_Nodes().get_Count() > 0) { // If the current node has child nodes, call the // CheckAllChildsNodes method recursively. this.CheckAllChildNodes(node, nodeChecked); } } } //CheckAllChildNodes // 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.get_Action().Equals(TreeViewAction.Unknown))) { if (e.get_Node().get_Nodes().get_Count() > 0) { /* Calls the CheckAllChildNodes method, passing in the current Checked value of the TreeNode whose checked state changed. */ this.CheckAllChildNodes(e.get_Node(), e.get_Node().get_Checked()); } } } //Node_AfterCheck

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からTreeView.BeforeCheck イベントを検索する場合は、下記のリンクをクリックしてください。

- TreeView.BeforeCheck イベントのページへのリンク