TreeNode.Clone Method

Definition

Copies the tree node and the entire subtree rooted at this tree node.

C#
public virtual object Clone();

Returns

The Object that represents the cloned TreeNode.

Implements

Examples

The following code example clones the last child tree node of the last root tree node and inserts the clone as the first root tree node in the TreeNodeCollection. This example requires that you have a TreeView control on a Form that contains a collection of TreeNode objects and a Button.

C#
private void button4_Click(object sender, System.EventArgs e)
{
   TreeNode lastNode = treeView1.Nodes[treeView1.Nodes.Count - 1].
     Nodes[treeView1.Nodes[treeView1.Nodes.Count - 1].Nodes.Count - 1];

   // Clone the last child node.
   TreeNode clonedNode = (TreeNode) lastNode.Clone();

   // Insert the cloned node as the first root node.
   treeView1.Nodes.Insert(0, clonedNode);
   MessageBox.Show(lastNode.Text + 
     " tree node cloned and added to " + treeView1.Nodes[0].Text);
}

Remarks

The tree structure from the tree node being cloned and below is copied. Any child tree nodes assigned to the TreeNode being cloned are included in the new tree node and subtree.

The Clone method performs a shallow copy of the node. If the value of the Tag property is a reference type, both the original and cloned copy will point to the same single instance of the Tag value.

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also