共用方式為


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,表示複製的 TreeNode

實作

範例

下列程式碼範例會複製最後一個根樹狀節點的最後一個子樹狀節點,並將複製專案插入 為 中的 TreeNodeCollection 第一個根樹狀節點。 這個範例會要求您在 上 Form 具有 TreeView 包含 物件集合 TreeNodeButton 的 控制項。

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

適用於

另請參閱