导航

TreeNode Tag 属性

Posted on 2009-05-21 11:32  kingwangzhen  阅读(1092)  评论(0编辑  收藏  举报

获取或设置包含树节点有关数据的对象。

任何Object 派生类型都可被指派给此属性。如果此属性是通过 Windows 窗体设计器设置的,则只能指派文本。

public class Customer
{
   public ArrayList CustomerOrders;
   public string CustomerName;
   public Customer(string myName)
   {
      CustomerName = myName;
      CustomerOrders = new ArrayList();
   }
}
public class Order
{
   public string OrderID;
   public Order(string myOrderID )
   {
      this.OrderID = myOrderID;
   }
}

public void AddRootNodes()
{
   // Add a root node to assign the customer nodes to.
   TreeNode rootNode = new TreeNode();
   rootNode.Text = "CustomerList";
   // Add a main root treenode.
   myTreeView.Nodes.Add(rootNode);

   // Add a root treenode for each 'Customer' object in the ArrayList.
   foreach(Customer myCustomer in customerArray)
   {
      // Add a child treenode for each Order object.
      int i = 0;
      TreeNode[] myTreeNodeArray = new TreeNode[5];
      foreach(Order myOrder in myCustomer.CustomerOrders)
      {
         myTreeNodeArray[i] = new TreeNode(myOrder.OrderID);
         i++;
      }
      TreeNode customerNode = new TreeNode(myCustomer.CustomerName,
        myTreeNodeArray);
        // Display the customer names with and Orange font.
        customerNode.ForeColor = Color.Orange;
        // Store the Customer object in the Tag property of the TreeNode.
        customerNode.Tag = myCustomer;
      myTreeView.Nodes[0].Nodes.Add(customerNode);
   }
}