Category / Section
How to expand all the child nodes of the selected TreeNodeAdv programatically in WinForms TreeViewAdv?
1 min read
Expand the child nodes of selected TreeNodeAdv
In the TreeViewAdv, TreeNodeAdv can be expanded by using the Expand and ExpandAll functions.
1. Expand: This function helps to expand a particular TreeNodeAdv.
2. ExpandAll: This function helps to expand the node and all its sub nodes.
To expand all the child nodes under the selected TreeNodeAdv programmatically, follow the steps:
1. Handle AfterSelect event that is triggered when the TreeNodeAdv is selected.
2. Iterate the TreeNodeAdvCollection and invoke the Expand function.
C#
//The event is raised on TreeNodeAdv selection. private void treeViewAdv1_AfterSelect(object sender, EventArgs e) { string selectedNode = this.treeViewAdv1.SelectedNode.Text; if (this.treeViewAdv1.Nodes.Count > 0) { foreach (TreeNodeAdv treeNode in Collect(this.treeViewAdv1.Nodes)) { if (selectedNode == treeNode.Text) { //Expands all the TreeNodeAdv. treeNode.ExpandAll(); break; } } } }
VB
'The event is raised on TreeNodeAdv selection. Private Sub treeViewAdv1_AfterSelect(ByVal sender As Object, ByVal e As EventArgs) Handles treeViewAdv1.AfterSelect Dim selectedNode As String = Me.treeViewAdv1.SelectedNode.Text If Me.treeViewAdv1.Nodes.Count > 0 Then For Each treeNode As TreeNodeAdv In Me.treeViewAdv1.Nodes If selectedNode = treeNode.Text Then 'Expands all the TreeNodeAdv. treeNode.ExpandAll() Exit For End If Next treeNode End If End Sub
Figure 1: Before expanding the TreeNodeAdv
Figure 2: After expanding the TreeNodeAdv on selection