TreeNodeCollection.Count 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得集合中物件的總數 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樹節點的列表。 此範例要求你有一個TreeView,且其 中至少有一個 TreeNodeCollectionTreeNode ,且 上有一個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 產生例外。