TreeNodeCollection.Count Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan jumlah TreeNode total objek dalam koleksi.
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
Nilai Properti
Jumlah TreeNode total objek dalam koleksi.
Penerapan
- Atribut
Contoh
Contoh kode berikut menampilkan jumlah TreeNode objek dalam TreeNodeCollection, menyalin konten koleksi ke Object array, dan menampilkan daftar simpul pohon dalam Label kontrol. Contoh ini mengharuskan Anda memiliki TreeView setidaknya satu TreeNode di , TreeNodeCollectiondan Label kontrol pada 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
Keterangan
Properti Count menyimpan jumlah TreeNode objek yang ditetapkan ke koleksi. Anda dapat menggunakan Count nilai properti sebagai batas atas perulangan untuk melakukan iterasi melalui koleksi.
Nota
Karena nilai indeks koleksi adalah indeks berbasis nol, Anda harus menguranginya dari variabel perulangan. Jika Anda tidak memperhitungkan hal ini, Anda akan melebihi batas atas koleksi dan melemparkan IndexOutOfRangeException pengecualian.