Freigeben über


TreeNodeCollection.Count-Eigenschaft

Ruft die Gesamtanzahl der TreeNode-Objekte in der Auflistung ab.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public ReadOnly Property Count As Integer
'Usage
Dim instance As TreeNodeCollection
Dim value As Integer

value = instance.Count
public int Count { get; }
public:
virtual property int Count {
    int get () sealed;
}
/** @property */
public final int get_Count ()
public final function get Count () : int

Eigenschaftenwert

Die Gesamtanzahl der TreeNode-Objekte in der Auflistung.

Hinweise

Die Count-Eigenschaft enthält die Anzahl der TreeNode-Objekte, die der Auflistung zugewiesen sind. Sie können den Count-Eigenschaftenwert als obere Grenze einer Schleife zum Durchlaufen einer Auflistung verwenden.

Hinweis

Da der Indexwert einer Auflistung nullbasiert ist, müssen Sie von der Schleifenvariable 1 subtrahieren. Bei Nichtbeachtung wird die obere Grenze der Auflistung überschritten und eine IndexOutOfRangeException-Ausnahme ausgelöst.

Beispiel

Im folgenden Codebeispiel wird die Anzahl der TreeNode-Objekte in einer TreeNodeCollection angezeigt und der Inhalt der Auflistung in ein Object-Array kopiert sowie eine Liste der Strukturknoten in einem Label-Steuerelement angezeigt. Für dieses Beispiel ist es erforderlich, dass eine TreeView mit mindestens einem TreeNode in der TreeNodeCollection und ein Label-Steuerelement in einem Form vorhanden ist.

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
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";
   }
}
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.get_Nodes();
    int myCount = myNodeCollection.get_Count();

    myLabel.set_Text(myLabel.get_Text()
        + "Number of nodes in the collection :" + myCount);
    myLabel.set_Text(myLabel.get_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.set_Text(myLabel.get_Text()
            + ((TreeNode)myArray[i]).get_Text() + "\n");
    }
} //CopyTreeNodes

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

TreeNodeCollection-Klasse
TreeNodeCollection-Member
System.Windows.Forms-Namespace