TreeNode.Clone 方法

定义

复制树节点和以此树节点为根的整个子树。

public:
 virtual System::Object ^ Clone();
public virtual object Clone ();
abstract member Clone : unit -> obj
override this.Clone : unit -> obj
Public Overridable Function Clone () As Object

返回

Object

表示克隆的 ObjectTreeNode

实现

示例

下面的代码示例克隆最后一个根树节点的最后一个子树节点,并将克隆作为第一个根树节点插入其中 TreeNodeCollection。 本示例要求对包含对象集合和对象的Button控件TreeNode具有一个TreeView控件Form

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 = dynamic_cast<TreeNode^>(lastNode->Clone());
   
   // Insert the cloned node as the first root node.
   treeView1->Nodes->Insert( 0, clonedNode );
   MessageBox::Show( String::Concat( lastNode->Text, " tree node cloned and added to ", treeView1->Nodes[ 0 ]->Text ) );
}
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);
}
Private Sub button4_Click(sender As Object, _
  e As System.EventArgs) Handles button4.Click
   Dim lastNode as TreeNode
   lastNode = treeView1.Nodes(treeView1.Nodes.Count - 1). _
     Nodes(treeView1.Nodes(treeView1.Nodes.Count - 1).Nodes.Count - 1)

   ' Clone the last child node.
   Dim clonedNode As TreeNode = CType(lastNode.Clone(), TreeNode)

   ' 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)
End Sub

注解

从要克隆的树节点和下面复制树结构。 分配给 TreeNode 所克隆的任何子树节点都包含在新树节点和子树中。

该方法 Clone 执行节点的浅表副本。 如果该属性的值 Tag 是引用类型,则原始副本和克隆副本都将指向该值的 Tag 同一个实例。

适用于

另请参阅