Export (0) Print
Expand All

TreeNode.Text Property

Gets or sets the text displayed in the label of the tree node.

[Visual Basic]
Public Property Text As String
[C#]
public string Text {get; set;}
[C++]
public: __property String* get_Text();
public: __property void set_Text(String*);
[JScript]
public function get Text() : String;
public function set Text(String);

Property Value

The text displayed in the label of the tree node.

Remarks

This property cannot be set by the user if the LabelEdit property of the parent TreeView is set to false. The alternative to setting this property explicitly is to create the tree node using one of the TreeNode constructors that has a string parameter that represents the Text property. The label is displayed next to the TreeNode image, if one is displayed.

Example

[Visual Basic, C#, C++] The following 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 assumes you have a Customer and Order object defined, a TreeView control on a Form, and an ArrayList named customerArray containing Customer objects.

[Visual Basic] 
Public Sub AddRootNodes()
   ' Add a root node to assign the customer nodes to.
   Dim rootNode As 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.
   Dim myCustomer As Customer
   For Each myCustomer In customerArray
      ' Add a child treenode for each Order object.
      Dim i As Integer = 0
      Dim myTreeNodeArray(4) As TreeNode
      Dim myOrder As Order
      For Each myOrder In  myCustomer.CustomerOrders
         myTreeNodeArray(i) = New TreeNode(myOrder.OrderID)
         i += 1
      Next myOrder
      Dim customerNode As 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)
   Next myCustomer
End Sub

[C#] 
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);
   }
}

[C++] 
public:
    void AddRootNodes() {
        // Add a root node to assign the customer nodes to.
        TreeNode* rootNode = new TreeNode();
        rootNode->Text = S"CustomerList";
        // Add a main root treenode.
        myTreeView->Nodes->Add(rootNode);

        // Add a root treenode for each 'Customer' object in the ArrayList.
        IEnumerator* myEnum = customerArray->GetEnumerator();
        while (myEnum->MoveNext()) {
            Customer* myCustomer = __try_cast<Customer*>(myEnum->Current);

            // Add a child treenode for each Order object.
            int i = 0;
            TreeNode* myTreeNodeArray[] = new TreeNode*[5];
            IEnumerator* myEnum = myCustomer->CustomerOrders->GetEnumerator();
            while (myEnum->MoveNext()) {
                Order* myOrder = __try_cast<Order*>(myEnum->Current);
                myTreeNodeArray->Item[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->Item[0]->Nodes->Add(customerNode);
        }
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

TreeNode Class | TreeNode Members | System.Windows.Forms Namespace | LabelEdit | BeginEdit | IsEditing

Show:
© 2014 Microsoft