Category / Section
How to copy the TreeNodeAdv and its sub nodes from WinForms TreeViewAdv to another TreeViewAdv?
1 min read
Copy the TreeNodeadv and sub nodes
The TreeNodeAdv and its sub nodes can be copied from one TreeViewAdv to another. By using the Clone function, the TreeNodes can be copied. The copied nodes can be added to another TreeViewAdv by using the Add function in the TreeNodeAdvCollection.
To copy the selected TreeNodeAdv: You can copy the selected TreeNodeAdv to another TreeViewAdv, by using the following code example.
C#
if (this.treeViewAdv1.SelectedNode != null) { // Copies the selected node. TreeNodeAdv copynodes = this.treeViewAdv1.SelectedNode.Clone(); // Adds the copied selected tree node to another TreeViewAdv. this.treeViewAdv2.Nodes.Add(copynodes); }
VB
If Me.treeViewAdv1.SelectedNode IsNot Nothing Then 'Copies the selected node. Dim copynodes As TreeNodeAdv = Me.treeViewAdv1.SelectedNode.Clone() 'Adds the copied selected tree node to another TreeViewAdv. Me.treeViewAdv2.Nodes.Add(copynodes) End If
To copy the TreeNodeAdv based on the index:
You can copy the TreeNodeAdv based on the index, and the copied tree node can be added to another TreeViewAdv by using the following code example.
C#
//Copies the first node based on the given index. TreeNodeAdv copynode = this.treeViewAdv1.Nodes[1].Clone(); //Adds the copied tree node to another TreeViewAdv. this.treeViewAdv2.Nodes.Add(copynode);
VB
'Copies the first node based on the given index. Dim copynode As TreeNodeAdv = Me.treeViewAdv1.Nodes(1).Clone() 'Adds the copied tree node to another TreeViewAdv. Me.treeViewAdv2.Nodes.Add(copynode)
Figure 1: Before adding TreeNodeAdv to another TreeViewAdv
Figure 2: After adding TreeNodeAdv to another TreeViewAdv
Samples:
C#: CopyTreeNode_C#
VB: CopyTreeNode_VB