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 物件總數。

實作

屬性

範例

下列程式碼範例會顯示 中的 TreeNodeCollection 物件數目 TreeNode ,將集合 Object 的內容複寫到陣列,並在 控制項中 Label 顯示樹狀節點的清單。 此範例要求您在其 TreeViewTreeNodeCollection 至少有一個 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 屬性值作為迴圈的上限,逐一查看集合。

注意

因為集合的索引值是以零起始的索引,所以您必須從迴圈變數中減去一個。 如果您不考慮這種情況,將會超過集合的上限並擲回 IndexOutOfRangeException 例外狀況。

適用於