TreeViewCancelEventArgs Class
Provides data for the BeforeCheck, BeforeCollapse, BeforeExpand, or BeforeSelect events of a TreeView control.
For a list of all members of this type, see TreeViewCancelEventArgs Members.
System.Object
System.EventArgs
System.ComponentModel.CancelEventArgs
System.Windows.Forms.TreeViewCancelEventArgs
[Visual Basic] Public Class TreeViewCancelEventArgs Inherits CancelEventArgs [C#] public class TreeViewCancelEventArgs : CancelEventArgs [C++] public __gc class TreeViewCancelEventArgs : public CancelEventArgs [JScript] public class TreeViewCancelEventArgs extends CancelEventArgs
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
For more information about handling events, see Consuming Events.
Example
[Visual Basic, C#, C++] The following example demonstrates how to change the collapse state of a TreeView so that all the checked nodes are visible. First, all the nodes are collapsed, and a handler is added to the TreeView.BeforeExpand event. Next, all the nodes are expanded. The TreeView.BeforeExpand event handler determines whether a given node has child nodes that are checked. If a node does not have checked children, the expansion is canceled for that node. In order to allow normal node expansion when the plus sign next to a node is clicked, the TreeView.BeforeExpand event handler is then removed.
[Visual Basic, C#, C++] This behavior can also be implemented by handling the TreeView.BeforeCollapse event, as illustrated in the example for that topic.
[Visual Basic, C#, C++] For the complete example, see the TreeView.CheckBoxes reference topic.
[Visual Basic] Private Sub showCheckedNodesButton_Click(ByVal sender As Object, ByVal e As EventArgs) ' 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. AddHandler treeView1.BeforeExpand, AddressOf 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. RemoveHandler treeView1.BeforeExpand, AddressOf CheckForCheckedChildren ' Enable redrawing of treeView1. treeView1.EndUpdate() End Sub 'showCheckedNodesButton_Click ' Prevent expansion of a node that does not have any checked child nodes. Private Sub CheckForCheckedChildren(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs) If Not HasCheckedChildNodes(e.Node) Then e.Cancel = True End If End Sub 'CheckForCheckedChildren ' Returns a value indicating whether the specified ' TreeNode has checked child nodes. Private Function HasCheckedChildNodes(ByVal node As TreeNode) As Boolean If node.Nodes.Count = 0 Then Return False End If Dim childNode As TreeNode For Each childNode In node.Nodes If childNode.Checked Then Return True End If ' Recursively check the children of the current child node. If HasCheckedChildNodes(childNode) Then Return True End If Next childNode Return False End Function 'HasCheckedChildNodes [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; } [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. 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. bool HasCheckedChildNodes(TreeNode* node) { if (node->Nodes->Count == 0) return false; System::Collections::IEnumerator* myEnum = node->Nodes->GetEnumerator(); while (myEnum->MoveNext()) { TreeNode* childNode = __try_cast<TreeNode*>(myEnum->Current); if (childNode->Checked) return true; // Recursively check the children of the current child node. if (HasCheckedChildNodes(childNode)) return true; } return false; }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Windows.Forms
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
See Also
TreeViewCancelEventArgs Members | System.Windows.Forms Namespace | TreeViewEventArgs