TreeNodeCollection 클래스
TreeNode 개체의 컬렉션을 나타냅니다.
네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)
구문
‘선언
Public Class TreeNodeCollection
Implements IList, ICollection, IEnumerable
‘사용 방법
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
설명
Add, Remove 및 RemoveAt 메서드를 사용하면 컬렉션에서 개별 트리 노드를 추가 및 제거할 수 있습니다.
참고
컬렉션 열거와 노드 제거는 지원되지 않습니다.
또한 AddRange 또는 Clear 메서드를 사용하면 컬렉션에서 모든 트리 노드를 추가하거나 제거할 수 있습니다.
클래스는 TreeNodeCollection 클래스에서 상속할 수 없습니다.
예제
다음 코드 예제에서는 TreeView 컨트롤에 고객 정보를 표시합니다. 루트 트리 노드에는 고객 이름이 표시되고 자식 트리 노드에는 각 고객에게 할당된 주문 번호가 표시됩니다. 이 예제에서는 주문을 15개씩 보유하고 있는 1000명의 고객이 표시됩니다. BeginUpdate 및 EndUpdate 메서드를 사용하여 TreeView의 다시 그리기가 억제되고 TreeView에서 TreeNode 개체를 만들고 그리는 동안 대기 중인 Cursor가 표시됩니다. 이 예제를 실행하려면 Order
개체의 컬렉션을 저장할 수 있는 Customer
개체가 있어야 합니다. 또한 Form에 TreeView 컨트롤의 인스턴스가 있어야 합니다.
' 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
상속 계층 구조
System.Object
System.Windows.Forms.TreeNodeCollection
스레드로부터의 안전성
이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.
플랫폼
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.
버전 정보
.NET Framework
2.0, 1.1, 1.0에서 지원
.NET Compact Framework
2.0, 1.0에서 지원
참고 항목
참조
TreeNodeCollection 멤버
System.Windows.Forms 네임스페이스
TreeView
TreeNode 클래스