This documentation is archived and is not being maintained.

TreeNode.Clone Method

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

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public virtual Object Clone()

Return Value

Type: System.Object
The Object that represents the cloned TreeNode.

Implements

ICloneable.Clone()

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

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.


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);
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: