次の方法で共有


TreeNodeCollection.CopyTo メソッド

コレクション全体を既存の配列内の指定した位置にコピーします。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

構文

'宣言
Public Sub CopyTo ( _
    dest As Array, _
    index As Integer _
)
'使用
Dim instance As TreeNodeCollection
Dim dest As Array
Dim index As Integer

instance.CopyTo(dest, index)
public void CopyTo (
    Array dest,
    int index
)
public:
virtual void CopyTo (
    Array^ dest, 
    int index
) sealed
public final void CopyTo (
    Array dest, 
    int index
)
public final function CopyTo (
    dest : Array, 
    index : int
)

パラメータ

  • dest
    コピー先の配列。
  • index
    コピー先の配列のコピー開始位置を示すインデックス。

使用例

TreeNodeCollectionTreeNode オブジェクトの数を表示し、コレクションの内容を Object 配列にコピーして、ツリー ノードのリストを Label コントロールに表示するコード例を次に示します。この例は、少なくとも 1 つの TreeNodeTreeViewTreeNodeCollection にあること、および Label コントロールが Form に配置されていることを前提にしています。

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
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";
   }
}
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.get_Nodes();
    int myCount = myNodeCollection.get_Count();

    myLabel.set_Text(myLabel.get_Text()
        + "Number of nodes in the collection :" + myCount);
    myLabel.set_Text(myLabel.get_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.set_Text(myLabel.get_Text()
            + ((TreeNode)myArray[i]).get_Text() + "\n");
    }
} //CopyTreeNodes

プラットフォーム

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0、1.1、1.0

.NET Compact Framework

サポート対象 : 2.0、1.0

参照

関連項目

TreeNodeCollection クラス
TreeNodeCollection メンバ
System.Windows.Forms 名前空間