TreeView.BeforeExpand Event
Occurs before the tree node is expanded.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
For more information about handling events, see Consuming Events.
The following code 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 for the BeforeExpand event. Next, all the nodes are expanded. The 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 standard node expansion when the plus sign next to a node is clicked, the BeforeExpand event handler is then removed.
This behavior can also be implemented by handling the BeforeCollapse event, as illustrated in the example for that topic.
For the complete example, see the CheckBoxes reference topic.
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
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.