TreeNodeCollection.Count Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Obtém o número total de TreeNode objetos na coleção.
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
Valor de Propriedade
O número total de TreeNode objetos na coleção.
Implementações
- Atributos
Exemplos
O seguinte exemplo de código mostra o número de TreeNode objetos num TreeNodeCollection, copia o conteúdo da coleção para um Object array e apresenta uma lista dos nós da árvore num Label controlo. Este exemplo exige que tenhas um TreeView com pelo menos um TreeNode em seu TreeNodeCollection, e um Label controlo sobre um 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
Observações
A Count propriedade contém o número de TreeNode objetos atribuídos à coleção. Podes usar o valor da Count propriedade como os limites superiores de um ciclo para iterar por uma coleção.
Note
Como o valor do índice de uma coleção é um índice baseado em zero, deve subtrair um da variável de looping. Se não tiveres isto em conta, vais ultrapassar os limites superiores da coleção e lançar uma IndexOutOfRangeException exceção.