TreeNodeCollection::CopyTo Method (Array^, Int32)

 

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

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public:
virtual void CopyTo(
	Array^ dest,
	int index
) sealed

Parameters

dest
Type: System::Array^

The destination array.

index
Type: System::Int32

The index in the destination array at which storing begins.

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. 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";

   }
}

.NET Framework
Available since 1.1
Return to top
Show: