TreeNodeCollection-Klasse
Stellt eine Auflistung von TreeNode-Objekten dar.
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)
Syntax
'Declaration
Public Class TreeNodeCollection
Implements IList, ICollection, IEnumerable
'Usage
Dim instance As TreeNodeCollection
public class TreeNodeCollection : IList, ICollection, IEnumerable
public ref class TreeNodeCollection : IList, ICollection, IEnumerable
public class TreeNodeCollection implements IList, ICollection,
IEnumerable
public class TreeNodeCollection implements IList, ICollection,
IEnumerable
Hinweise
Mithilfe der Add-Methode, der Remove-Methode und der RemoveAt-Methode können Sie einzelne Strukturknoten zur Auflistung hinzufügen oder daraus entfernen.
Hinweis
Das Enumerieren der Auflistung und Entfernen von Knoten wird nicht unterstützt.
Darüber hinaus können Sie mit der AddRange-Methode oder der Clear-Methode alle Strukturknoten zur Auflistung hinzufügen oder daraus entfernen.
Andere Klassen können nicht von der TreeNodeCollection-Klasse erben.
Beispiel
Im folgenden Codebeispiel werden Kundeninformationen in einem TreeView-Steuerelement angezeigt. Die Stammknoten zeigen die Namen der Kunden, die untergeordneten Knoten zeigen die jedem Kunden zugewiesenen Auftragsnummern an. In diesem Beispiel werden 1.000 Kunden mit jeweils 15 Aufträgen angezeigt. Das Neuaufbauen des TreeView wird mithilfe der BeginUpdate-Methode und der EndUpdate-Methode unterdrückt. Ein Warte-Cursor wird angezeigt, solange die TreeNode-Objekte im TreeView erstellt und gezeichnet werden. Für dieses Beispiel muss ein Customer
-Objekt vorhanden sein, das eine Auflistung von Order
-Objekten enthalten kann. Des Weiteren muss in einem Form eine Instanz eines TreeView-Steuerelements erstellt worden sein.
' Create a new ArrayList to hold the Customer objects.
Private customerArray As New ArrayList()
Private Sub FillMyTreeView()
' Add customers to the ArrayList of Customer objects.
Dim x As Integer
For x = 0 To 999
customerArray.Add(New Customer("Customer" + x.ToString()))
Next x
' Add orders to each Customer object in the ArrayList.
Dim customer1 As Customer
For Each customer1 In customerArray
Dim y As Integer
For y = 0 To 14
customer1.CustomerOrders.Add(New Order("Order" + y.ToString()))
Next y
Next customer1
' Display a wait cursor while the TreeNodes are being created.
Cursor.Current = New Cursor("MyWait.cur")
' Suppress repainting the TreeView until all the objects have been created.
treeView1.BeginUpdate()
' Clear the TreeView each time the method is called.
treeView1.Nodes.Clear()
' Add a root TreeNode for each Customer object in the ArrayList.
Dim customer2 As Customer
For Each customer2 In customerArray
treeView1.Nodes.Add(New TreeNode(customer2.CustomerName))
' Add a child TreeNode for each Order object in the current Customer object.
Dim order1 As Order
For Each order1 In customer2.CustomerOrders
treeView1.Nodes(customerArray.IndexOf(customer2)).Nodes.Add( _
New TreeNode(customer2.CustomerName + "." + order1.OrderID))
Next order1
Next customer2
' Reset the cursor to the default for all controls.
Cursor.Current = System.Windows.Forms.Cursors.Default
' Begin repainting the TreeView.
treeView1.EndUpdate()
End Sub 'FillMyTreeView
// Create a new ArrayList to hold the Customer objects.
private ArrayList customerArray = new ArrayList();
private void FillMyTreeView()
{
// Add customers to the ArrayList of Customer objects.
for(int x=0; x<1000; x++)
{
customerArray.Add(new Customer("Customer" + x.ToString()));
}
// Add orders to each Customer object in the ArrayList.
foreach(Customer customer1 in customerArray)
{
for(int y=0; y<15; y++)
{
customer1.CustomerOrders.Add(new Order("Order" + y.ToString()));
}
}
// Display a wait cursor while the TreeNodes are being created.
Cursor.Current = new Cursor("MyWait.cur");
// Suppress repainting the TreeView until all the objects have been created.
treeView1.BeginUpdate();
// Clear the TreeView each time the method is called.
treeView1.Nodes.Clear();
// Add a root TreeNode for each Customer object in the ArrayList.
foreach(Customer customer2 in customerArray)
{
treeView1.Nodes.Add(new TreeNode(customer2.CustomerName));
// Add a child treenode for each Order object in the current Customer object.
foreach(Order order1 in customer2.CustomerOrders)
{
treeView1.Nodes[customerArray.IndexOf(customer2)].Nodes.Add(
new TreeNode(customer2.CustomerName + "." + order1.OrderID));
}
}
// Reset the cursor to the default for all controls.
Cursor.Current = Cursors.Default;
// Begin repainting the TreeView.
treeView1.EndUpdate();
}
void FillMyTreeView()
{
// Add customers to the ArrayList of Customer objects.
for ( int x = 0; x < 1000; x++ )
{
customerArray->Add( gcnew Customer( "Customer " + x ) );
}
// Add orders to each Customer object in the ArrayList.
IEnumerator^ myEnum = customerArray->GetEnumerator();
while ( myEnum->MoveNext() )
{
Customer^ customer1 = safe_cast<Customer^>(myEnum->Current);
for ( int y = 0; y < 15; y++ )
{
customer1->CustomerOrders->Add( gcnew Order( "Order " + y ) );
}
}
// Display a wait cursor while the TreeNodes are being created.
::Cursor::Current = gcnew System::Windows::Forms::Cursor( "MyWait.cur" );
// Suppress repainting the TreeView until all the objects have been created.
treeView1->BeginUpdate();
// Clear the TreeView each time the method is called.
treeView1->Nodes->Clear();
// Add a root TreeNode for each Customer object in the ArrayList.
while ( myEnum->MoveNext() )
{
Customer^ customer2 = safe_cast<Customer^>(myEnum->Current);
treeView1->Nodes->Add( gcnew TreeNode( customer2->CustomerName ) );
// Add a child treenode for each Order object in the current Customer object.
IEnumerator^ myEnum = customer2->CustomerOrders->GetEnumerator();
while ( myEnum->MoveNext() )
{
Order^ order1 = safe_cast<Order^>(myEnum->Current);
treeView1->Nodes[ customerArray->IndexOf( customer2 ) ]->Nodes->Add( gcnew TreeNode( customer2->CustomerName + "." + order1->OrderID ) );
}
}
// Reset the cursor to the default for all controls.
::Cursor::Current = Cursors::Default;
// Begin repainting the TreeView.
treeView1->EndUpdate();
}
// Create a new ArrayList to hold the Customer objects.
private ArrayList customerArray = new ArrayList();
private void FillMyTreeView()
{
// Add customers to the ArrayList of Customer objects.
for (int x = 0; x < 1000; x++) {
customerArray.Add(new Customer("Customer"
+ ((Int32)x).ToString()));
}
// Add orders to each Customer object in the ArrayList.
for (int iCtr = 0; iCtr < customerArray.get_Count(); iCtr++) {
Customer customer1 = (Customer)customerArray.get_Item(iCtr);
for (int y = 0; y < 15; y++) {
customer1.get_CustomerOrders().Add(new Order("Order"
+ ((Int32)y).ToString()));
}
}
// Display a wait cursor while the TreeNodes are being created.
get_Cursor().set_Current(new Cursor("MyWait.cur"));
// Suppress repainting the TreeView until all the objects have
// been created.
treeView1.BeginUpdate();
// Clear the TreeView each time the method is called.
treeView1.get_Nodes().Clear();
// Add a root TreeNode for each Customer object in the ArrayList.
for (int iCtr1 = 0; iCtr1 < customerArray.get_Count(); iCtr1++) {
Customer customer2 = (Customer)customerArray.get_Item(iCtr1);
treeView1.get_Nodes().Add(new TreeNode(customer2.get_CustomerName()));
// Add a child treenode for each Order object in the current
// Customer object.
for (int iCtr2 = 0; iCtr2 < customer2.get_CustomerOrders().
get_Count(); iCtr2++) {
Order order1 = (Order)customer2.get_CustomerOrders().
get_Item(iCtr2);
treeView1.get_Nodes().
get_Item(customerArray.IndexOf(customer2)).get_Nodes().
Add(new TreeNode(customer2.get_CustomerName() + "."
+ order1.get_OrderID()));
}
}
// Reset the cursor to the default for all controls.
get_Cursor().set_Current(Cursors.get_Default());
// Begin repainting the TreeView.
treeView1.EndUpdate();
} //FillMyTreeView
Vererbungshierarchie
System.Object
System.Windows.Forms.TreeNodeCollection
Threadsicherheit
Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.
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-Member
System.Windows.Forms-Namespace
TreeView
TreeNode-Klasse