TreeNodeCollection.Add Method (String)


Adds a new tree node with the specified label text to the end of the current tree node collection.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public virtual TreeNode Add(
	string text


Type: System.String

The label text displayed by the TreeNode.

Return Value

Type: System.Windows.Forms.TreeNode

A TreeNode that represents the tree node being added to the collection.

You can also add new TreeNode objects to the collection by using the AddRange or Insert methods.

To remove a TreeNode that you previously added, use the Remove, RemoveAt, or Clear methods.

The following code example displays customer information in a TreeView control. The root tree nodes display customer names, and the child tree nodes display the order numbers assigned to each customer. In this example, 1,000 customers are displayed with 15 orders each. The repainting of the TreeView is suppressed by using the BeginUpdate and EndUpdate methods, and a wait Cursor is displayed while the TreeView creates and paints the TreeNode objects. This example requires that you have a Customer object that can hold a collection of Order objects. It also requires that you have created an instance of a TreeView control on a Form.

// The basic Customer class.
public class Customer : System.Object
   private string custName = "";
   protected ArrayList custOrders = new ArrayList();

   public Customer(string customername)
      this.custName = customername;

   public string CustomerName
      get{return this.custName;}
      set{this.custName = value;}

   public ArrayList CustomerOrders 
      get{return this.custOrders;}

} // End Customer class 

// The basic customer Order class.
public class Order : System.Object
   private string ordID = "";

   public Order(string orderid)
      this.ordID = orderid;

   public string OrderID
      get{return this.ordID;}
      set{this.ordID = value;}

} // End Order class

// 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.

   // Clear the TreeView each time the method is called.

   // 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)
           new TreeNode(customer2.CustomerName + "." + order1.OrderID));

   // Reset the cursor to the default for all controls.
   Cursor.Current = Cursors.Default;

   // Begin repainting the TreeView.

.NET Framework
Available since 1.1
Return to top