TreeNodeCollection.CopyTo(Array, Int32) 方法

定义

将整个集合复制到现有数组中,从该数组内的指定位置开始复制。

public:
 virtual void CopyTo(Array ^ dest, int index);
public void CopyTo (Array dest, int index);
abstract member CopyTo : Array * int -> unit
override this.CopyTo : Array * int -> unit
Public Sub CopyTo (dest As Array, index As Integer)

参数

dest
Array

目标数组。

index
Int32

目标数组中的索引,存储从此处开始。

实现

示例

下面的代码示例显示集合TreeNodeCollection中的对象数TreeNode,将集合的内容复制到Object数组中,并在控件中Label显示树节点的列表。 本示例要求具有至少一 TreeViewTreeNode 控件 TreeNodeCollection,并且 Label 控件位于 Form其中。

void CopyTreeNodes()
{
   // Get the collection of TreeNodes.
   TreeNodeCollection^ myNodeCollection = myTreeView->Nodes;
   int myCount = myNodeCollection->Count;
   myLabel->Text = String::Concat( myLabel->Text, "Number of nodes in the collection : ", myCount );
   myLabel->Text = String::Concat( myLabel->Text, "\n\nElements of the Array after Copying from the collection :\n" );
   
   // Create an Object array.
   array<Object^>^myArray = gcnew array<Object^>(myCount);
   
   // Copy the collection into an array.
   myNodeCollection->CopyTo( myArray, 0 );
   for ( int i = 0; i < myArray->Length; i++ )
   {
      myLabel->Text = myLabel->Text + (dynamic_cast<TreeNode^>(myArray[ i ]))->Text + "\n";

   }
}
private void CopyTreeNodes()
{
   // Get the collection of TreeNodes.
   TreeNodeCollection myNodeCollection = myTreeView.Nodes;
   int myCount = myNodeCollection.Count;

   myLabel.Text += "Number of nodes in the collection :" + myCount;
   myLabel.Text += "\n\nElements of the Array after Copying from the collection :\n";
   // Create an Object array.
   Object[] myArray = new Object[myCount];
   // Copy the collection into an array.
   myNodeCollection.CopyTo(myArray,0);
   for(int i=0; i<myArray.Length; i++)
   {
      myLabel.Text += ((TreeNode)myArray[i]).Text + "\n";
   }
}
Private Sub CopyTreeNodes()
   ' Get the collection of TreeNodes.
   Dim myNodeCollection As TreeNodeCollection = myTreeView.Nodes
   Dim myCount As Integer = myNodeCollection.Count

   myLabel.Text += "Number of nodes in the collection :" + myCount.ToString()

   myLabel.Text += ControlChars.NewLine + ControlChars.NewLine + _
     "Elements of the Array after Copying from the collection :" + ControlChars.NewLine

   ' Create an Object array.
   Dim myArray(myCount -1) As Object

   ' Copy the collection into an array.
   myNodeCollection.CopyTo(myArray, 0)
   Dim i As Integer
   For i = 0 To myArray.Length - 1
      myLabel.Text += CType(myArray(i), TreeNode).Text + ControlChars.NewLine
   Next i
End Sub

适用于