How to Populate the ParentNode and ChildNode for the Selected Node Programmatically

1 Oct 20181 minute to read

This can be done through code as shown below, by setting the selected node to be the present node and then if the present node is equal to null, the new ChildNode is created and added.

private void button1_Click(object sender, System.EventArgs e) 
    TreeNodeAdv presentNode = this.treeViewAdv1.SelectedNode; 
    if(presentNode == null) 

// Adding new child nodes.
    TreeNodeAdv newNode = new TreeNodeAdv("NewChild"); 
private void button2_Click(object sender, System.EventArgs e) 

// Adding new parent nodes.
     TreeNodeAdv node = new TreeNodeAdv("Parent node"); 
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
Dim presentNode As TreeNodeAdv = Me.treeViewAdv1.SelectedNode 
If presentNode Is Nothing Then 
End If 

' Adding new child nodes.
Dim newNode As TreeNodeAdv = New TreeNodeAdv("NewChild")presentNode.Nodes.Add(newNode) 
End Sub 
Private Sub button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) 

' Adding new parent nodes.
Dim node As TreeNodeAdv = New TreeNodeAdv("Parent node") 
End Sub