TreeNodeCollection.CopyTo(Array, Int32) TreeNodeCollection.CopyTo(Array, Int32) TreeNodeCollection.CopyTo(Array, Int32) TreeNodeCollection.CopyTo(Array, Int32) Method

定義

コレクション全体を既存の配列内の指定した位置にコピーします。Copies the entire collection into an existing array at a specified location within the array.

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 Array Array Array

コピー先の配列。The destination array.

index
Int32 Int32 Int32 Int32

コピー先の配列のコピー開始位置を示すインデックス。The index in the destination array at which storing begins.

実装

次のコードTreeNode例では、 TreeNodeCollection内のオブジェクトの数を表示し、コレクションObjectの内容を配列にコピーして、 Labelコントロール内のツリーノードの一覧を表示します。The following code example displays the number of TreeNode objects in a TreeNodeCollection, copies the contents of the collection to an Object array, and displays a list of the tree nodes in a Label control. この例TreeViewでは、に少なくとも1つTreeNode TreeNodeCollection Labelのがあり、にFormコントロールがあることが必要です。This example requires that you have a TreeView with at least one TreeNode in its TreeNodeCollection, and a Label control on a 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

適用対象