다음을 통해 공유


TreeNodeCollection.Count 속성

정의

컬렉션의 총 개체 수를 TreeNode 가져옵니다.

public:
 property int Count { int get(); };
[System.ComponentModel.Browsable(false)]
public int Count { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Count : int
Public ReadOnly Property Count As Integer

속성 값

컬렉션의 TreeNode 총 개체 수입니다.

구현

특성

예제

다음 코드 예제에서는 개체 수를 TreeNode 표시하고 컬렉션의 내용을 배열에 Object 복사한 다음 컨트롤의 트리 노드 목록을 표시합니다Label.TreeNodeCollection 이 예제에서는 TreeView 해당 항목에 TreeNodeCollection하나 TreeNode 이상의 컨트롤과 컨트롤이 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

설명

이 속성은 Count 컬렉션에 할당된 개체 수를 TreeNode 보유합니다. 이 속성 값을 루프의 상한으로 사용하여 Count 컬렉션을 반복할 수 있습니다.

메모

컬렉션의 인덱스 값은 0부터 시작하는 인덱스이므로 루프 변수에서 인덱스 값을 빼야 합니다. 이를 고려하지 않으면 컬렉션의 상한을 초과하고 예외를 throw합니다 IndexOutOfRangeException .

적용 대상