TreeView.BeforeCollapse Event
Assembly: System.Windows.Forms (in system.windows.forms.dll)
'Declaration Public Event BeforeCollapse As TreeViewCancelEventHandler 'Usage Dim instance As TreeView Dim handler As TreeViewCancelEventHandler AddHandler instance.BeforeCollapse, handler
/** @event */ public void add_BeforeCollapse (TreeViewCancelEventHandler value) /** @event */ public void remove_BeforeCollapse (TreeViewCancelEventHandler value)
JScript supports the use of events, but not the declaration of new ones.
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 expanded, and a handler is added for the BeforeCollapse event. Next, all the nodes are collapsed. The BeforeCollapse event handler determines whether a given node has child nodes that are checked. If a node has checked children, the collapse is canceled for that node. In order to allow standard node collapse when the minus sign next to a node is clicked, the BeforeCollapse event handler is then removed.
This behavior can also be implemented by handling the BeforeExpand event, as illustrated in the example for that topic.
For the complete example, see the TreeView constructor 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.ExpandAll() ' Add the CheckForCheckedChildren event handler to the BeforeExpand event. AddHandler treeView1.BeforeCollapse, AddressOf CheckForCheckedChildren ' Expand all nodes of treeView1. Nodes without checked children are ' prevented from expanding by the checkForCheckedChildren event handler. treeView1.CollapseAll() ' Remove the checkForCheckedChildren event handler from the BeforeExpand ' event so manual node expansion will work correctly. RemoveHandler treeView1.BeforeCollapse, AddressOf CheckForCheckedChildren ' Enable redrawing of treeView1. treeView1.EndUpdate() End Sub 'showCheckedNodesButton_Click ' Prevent collapse of a node that has checked child nodes. Private Sub CheckForCheckedChildren(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs) If 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
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.ExpandAll();
// Add the checkForCheckedChildren event handler to the
// BeforeExpand event.
treeView1.add_BeforeCollapse(checkForCheckedChildren);
// Expand all nodes of treeView1. Nodes without checked children are
// prevented from expanding by the checkForCheckedChildren event handler.
treeView1.CollapseAll();
// Remove the checkForCheckedChildren event handler from the
// BeforeExpand event so manual node expansion will work correctly.
treeView1.remove_BeforeCollapse(checkForCheckedChildren);
// Enable redrawing of treeView1.
treeView1.EndUpdate();
} //showCheckedNodesButton_Click
// Prevent collapse of a node that has checked child nodes.
private void CheckForCheckedChildrenHandler(Object sender,
TreeViewCancelEventArgs e)
{
if (HasCheckedChildNodes(e.get_Node())) {
e.set_Cancel(true);
}
} //CheckForCheckedChildrenHandler
// Returns a value indicating whether the specified
// TreeNode has checked child nodes.
private boolean HasCheckedChildNodes(TreeNode node)
{
if (node.get_Nodes().get_Count() == 0) {
return false;
}
for (int iCtr = 0; iCtr < node.get_Nodes().get_Count(); iCtr++) {
TreeNode childNode = node.get_Nodes().get_Item(iCtr);
if (childNode.get_Checked()) {
return true;
}
// Recursively check the children of the current child node.
if (HasCheckedChildNodes(childNode)) {
return true;
}
}
return false;
} //HasCheckedChildNodes
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
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.