Export (0) Print
Expand All

TreeView.BeforeCollapse Event

Occurs before the tree node is collapsed.

[Visual Basic]
Public Event BeforeCollapse As TreeViewCancelEventHandler
[C#]
public event TreeViewCancelEventHandler BeforeCollapse;
[C++]
public: __event TreeViewCancelEventHandler* BeforeCollapse;

[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

The event handler receives an argument of type TreeViewCancelEventArgs containing data related to this event. The following TreeViewCancelEventArgs properties provide information specific to this event.

Property Description
Action Gets the type of TreeViewAction that raised the event.
Cancel (inherited from CancelEventArgs) Gets or sets a value indicating whether the event should be canceled.
Node Gets the tree node to be checked, expanded, collapsed, or selected.

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 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 normal node collapse when the minus sign next to a node is clicked, the BeforeCollapse event handler is then removed.

[Visual Basic, C#, C++] This behavior can also be implemented by handling the BeforeExpand event, as illustrated in the example for that topic.

[Visual Basic, C#, C++] For the complete example, see the TreeView constructor 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.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

[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.ExpandAll();

    // Add the checkForCheckedChildren event handler to the BeforeExpand event.
    treeView1.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.BeforeCollapse -= checkForCheckedChildren;

    // Enable redrawing of treeView1.
    treeView1.EndUpdate();
}

// Prevent collapse of a node that has 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->ExpandAll();

        // Add the checkForCheckedChildren event handler to the BeforeExpand event.
        treeView1->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->BeforeCollapse -= checkForCheckedChildren;

        // Enable redrawing of treeView1.
        treeView1->EndUpdate();
    }

    // Prevent collapse of a node that has 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 Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

TreeView Class | TreeView Members | System.Windows.Forms Namespace | OnBeforeCollapse | AfterCollapse | OnAfterCollapse

Show:
© 2015 Microsoft