This documentation is archived and is not being maintained.

TreeNode.Tag Property

Gets or sets the object that contains data about the tree node.

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

[LocalizableAttribute(false)] 
[BindableAttribute(true)] 
[TypeConverterAttribute(typeof(StringConverter))] 
public Object Tag { get; set; }
/** @property */
public Object get_Tag ()

/** @property */
public void set_Tag (Object value)

public function get Tag () : Object

public function set Tag (value : Object)

Not applicable.

Property Value

An Object that contains data about the tree node. The default is a null reference (Nothing in Visual Basic).

Any Object derived type can be assigned to this property. If this property is being set through the Windows Forms designer, only text can be assigned.

The following code example creates a root tree node to assign child tree nodes to. A child tree node for each Customer object in an ArrayList is added to the root tree node as well as a child tree node for each Order object assigned to the Customer object. The Customer object is assigned to the Tag property, and the tree nodes representing Customer objects are displayed with Orange text. This example requires that you have a Customer and Order object defined, a TreeView control on a Form, and an ArrayList named customerArray containing Customer objects.

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);
   }
}

public void AddRootNodes()
{
    // Add a root node to assign the customer nodes to.
    TreeNode rootNode = new TreeNode();
    rootNode.set_Text("CustomerList");
    // Add a main root treenode.
    myTreeView.get_Nodes().Add(rootNode);
    // Add a root treenode for each 'Customer' object in the ArrayList.
    for (int iCtr1 = 0; iCtr1 < customerArray.get_Count(); iCtr1++) {
        Customer myCustomer = (Customer)customerArray.get_Item(iCtr1);
        // Add a child treenode for each Order object.
        int i = 0;
        TreeNode myTreeNodeArray[] = new TreeNode[5];
        for (int iCtr2 = 0; iCtr2 < myCustomer.customerOrders.get_Count();
            iCtr2++) {
            Order myOrder = (Order)myCustomer.customerOrders.get_Item(iCtr2);
            myTreeNodeArray.set_Item(i, new TreeNode(myOrder.orderID));
            i++;
        }
        TreeNode customerNode =
            new TreeNode(myCustomer.customerName, myTreeNodeArray);
        // Display the customer names with and Orange font.
        customerNode.set_ForeColor(Color.get_Orange());
        // Store the Customer object in the Tag property of the TreeNode.
        customerNode.set_Tag(myCustomer);
        myTreeView.get_Nodes().get_Item(0).get_Nodes().Add(customerNode);
    }
} //AddRootNodes

Windows 98, Windows Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Show: