Export (0) Print
Expand All

TreeNodeCollection.CopyTo Method

Copies the entire collection into an existing array at a specified location within the array.

[Visual Basic]
Public Overridable Sub CopyTo( _
   ByVal dest As Array, _
   ByVal index As Integer _
) Implements ICollection.CopyTo
[C#]
public virtual void CopyTo(
 Array dest,
 int index
);
[C++]
public: virtual void CopyTo(
 Array* dest,
 int index
);
[JScript]
public function CopyTo(
   dest : Array,
 index : int
);

Parameters

dest
The destination array.
index
The index in the destination array at which storing begins.

Implements

ICollection.CopyTo

Example

[Visual Basic, C#, C++] The following example displays the number of TreeNode objects in a TreeNodeCollection and copies the contents of the collection to an Object array and displays a list of the tree nodes in a Label control. This example assumes you have a TreeView with at least one TreeNode in its TreeNodeCollection and a Label control on a Form.

[Visual Basic] 
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

[C#] 
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";
   }
}

[C++] 
private:
    void CopyTreeNodes() {
        // Get the collection of TreeNodes.
        TreeNodeCollection* myNodeCollection = myTreeView->Nodes;
        int myCount = myNodeCollection->Count;

        myLabel->Text = String::Concat(myLabel->Text, 
            S"Number of nodes in the collection : ", __box(myCount));
        myLabel->Text = String::Concat(myLabel->Text, 
            S"\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 = String::Concat(myLabel->Text, 
                (dynamic_cast<TreeNode*>(myArray->Item[i]))->Text, S"\n");
        }
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

TreeNodeCollection Class | TreeNodeCollection Members | System.Windows.Forms Namespace

Show:
© 2015 Microsoft