TreeView for WinForms | ComponentOne
Node Operations / Expand and Collapse Nodes
In This Topic
    Expand and Collapse Nodes
    In This Topic

    Expanding Nodes

    The C1TreeNode class provides the Expand method to expand a single node (parent or child). The Expand method accepts Boolean values to determine whether the child nodes within a particular node should expand or not. On setting this method to true, all the child nodes along with the selected node gets expanded. If set to false, only the node at which the method is called gets expanded. The following code snippet demonstrates how to expand a single node using the Expand method.  

    ' expand the first parent node without expanding its child nodes
    parentNode1.Expand(False)
    
    // expand the first parent node without expanding its child nodes
    parentNode1.Expand(false);
    

     The parent node gets expanded without expanding the child nodes, as shown below.

     Expanded parent node

    The C1TreeNode class also provides the Expanded property to expand a single node at a time.

    Additionally, the C1TreeView class provides the ExpandAll method to fully expand a TreeView. In fully expanded state, the TreeView displays all the children nodes under a parent node as shown in the image below.  

    Expanded nodes

    The following code snippet demonstrates how to fully expand a TreeView using the ExpandAll method. 

    ' call the ExpandAll method
    C1TreeView1.ExpandAll()
    
    // call the ExpandAll method
    c1TreeView1.ExpandAll();
    

    Once the TreeView is in fully expanded state, you can prevent it from collapsing. This can be achieved by cancelling the Collapsing event of the C1TreeView class as demonstrated in the code below.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' call the ExpandAll method
        C1TreeView1.ExpandAll()
    
        ' handle the Collapsing event
        AddHandler C1TreeView1.Collapsing, AddressOf C1TreeView1_Collapsing
    End Sub
    
    Private Sub C1TreeView1_Collapsing(sender As Object, e As C1TreeViewCancelEventArgs)
    
        ' cancel the event
        e.Cancel = True
    End Sub
    
    private void Form1_Load(object sender, EventArgs e)
    {
        // call the ExpandAll method
        c1TreeView1.ExpandAll();
    
        // handle the Collapsing event
        c1TreeView1.Collapsing += C1TreeView1_Collapsing; ;
    }
    
    private void C1TreeView1_Collapsing(object sender, C1.Win.TreeView.C1TreeViewCancelEventArgs e)
    {
        // cancel the event
        e.Cancel = true;
    }
    

    Collapsing Nodes

    The C1TreeView class provides the CollapseAll method to fully collapse a TreeView. To collapse a single node (parent or child), the C1TreeNode class provides the Collapse method. This method accepts Boolean values to determine whether the child nodes within a particular node should collapse or not. On setting this method to true, all the child nodes along with the selected node gets collapsed. If set to false, only the node at which the method is called gets collapsed.

    See Also