Export (0) Print
Expand All

TreeNode Constructor

.NET Framework 1.1

Initializes a new instance of the TreeNode class.

Overload List

Initializes a new instance of the TreeNode class.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New()
[C#] public TreeNode();
[C++] public: TreeNode();
[JScript] public function TreeNode();

Initializes a new instance of the TreeNode class with the specified label text.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String)
[C#] public TreeNode(string);
[C++] public: TreeNode(String*);
[JScript] public function TreeNode(String);

Initializes a new instance of the TreeNode class with the specified label text and child tree nodes.

[Visual Basic] Public Sub New(String, TreeNode())
[C#] public TreeNode(string, TreeNode[]);
[C++] public: TreeNode(String*, TreeNode[]);
[JScript] public function TreeNode(String, TreeNode[]);

Initializes a new instance of the TreeNode class with the specified label text and images to display when the tree node is in a selected and unselected state.

[Visual Basic] Public Sub New(String, Integer, Integer)
[C#] public TreeNode(string, int, int);
[C++] public: TreeNode(String*, int, int);
[JScript] public function TreeNode(String, int, int);

Initializes a new instance of the TreeNode class with the specified label text, child tree nodes, and images to display when the tree node is in a selected and unselected state.

[Visual Basic] Public Sub New(String, Integer, Integer, TreeNode())
[C#] public TreeNode(string, int, int, TreeNode[]);
[C++] public: TreeNode(String*, int, int, TreeNode[]);
[JScript] public function TreeNode(String, int, int, TreeNode[]);

Example

[Visual Basic, C#, C++] The following example creates and assigns an ImageList to a TreeView control and fills the TreeView control with TreeNode objects. The tree nodes are assigned images from the ImageList that are displayed when in a seleced or unselected state. This example assumes you have a Form containing a TreeView, and an ArrayList containing Customer objects that each contain Order objects. It is also assumed that the Customer and Order objects are defined.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the TreeNode constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Private Sub FillTreeView()
   ' Load the images in an ImageList.
   Dim myImageList As New ImageList()
   myImageList.Images.Add(Image.FromFile("Default.gif"))
   myImageList.Images.Add(Image.FromFile("SelectedDefault.gif"))
   myImageList.Images.Add(Image.FromFile("Root.gif"))
   myImageList.Images.Add(Image.FromFile("UnselectedCustomer.gif"))
   myImageList.Images.Add(Image.FromFile("SelectedCustomer.gif"))
   myImageList.Images.Add(Image.FromFile("UnselectedOrder.gif"))
   myImageList.Images.Add(Image.FromFile("SelectedOrder.gif"))
   
   ' Assign the ImageList to the TreeView.
   myTreeView.ImageList = myImageList
   
   ' Set the TreeView control's default image and selected image indexes.
   myTreeView.ImageIndex = 0
   myTreeView.SelectedImageIndex = 1
   
   ' Set the index of image from the 
   ' ImageList for selected and unselected tree nodes.
   Me.rootImageIndex = 2
   Me.selectedCustomerImageIndex = 3
   Me.unselectedCustomerImageIndex = 4
   Me.selectedOrderImageIndex = 5
   Me.unselectedOrderImageIndex = 6
   
   ' Create the root tree node.
   Dim rootNode As New TreeNode("CustomerList")
   rootNode.ImageIndex = rootImageIndex
   rootNode.SelectedImageIndex = rootImageIndex
   
   ' Add a main root tree node.
   myTreeView.Nodes.Add(rootNode)
   
   ' Add a root tree node for each Customer object in the ArrayList.
   Dim myCustomer As Customer
   For Each myCustomer In  customerArray
      ' Add a child tree node for each Order object.
      Dim countIndex As Integer = 0
      Dim myTreeNodeArray(myCustomer.CustomerOrders.Count) As TreeNode
      Dim myOrder As Order
      For Each myOrder In  myCustomer.CustomerOrders
         ' Add the Order tree node to the array.
         myTreeNodeArray(countIndex) = New TreeNode(myOrder.OrderID, _
            unselectedOrderImageIndex, selectedOrderImageIndex)
         countIndex += 1
      Next myOrder
      ' Add the Customer tree node.
      Dim customerNode As New TreeNode(myCustomer.CustomerName, _
         unselectedCustomerImageIndex, selectedCustomerImageIndex, myTreeNodeArray)
      myTreeView.Nodes(0).Nodes.Add(customerNode)
   Next myCustomer
End Sub

[C#] 
private void FillTreeView()
{
    // Load the images in an ImageList.
    ImageList myImageList = new ImageList();
    myImageList.Images.Add(Image.FromFile("Default.gif"));
    myImageList.Images.Add(Image.FromFile("SelectedDefault.gif"));
    myImageList.Images.Add(Image.FromFile("Root.gif"));
    myImageList.Images.Add(Image.FromFile("UnselectedCustomer.gif"));
    myImageList.Images.Add(Image.FromFile("SelectedCustomer.gif"));
    myImageList.Images.Add(Image.FromFile("UnselectedOrder.gif"));
    myImageList.Images.Add(Image.FromFile("SelectedOrder.gif"));
    
    // Assign the ImageList to the TreeView.
    myTreeView.ImageList = myImageList;
    
    // Set the TreeView control's default image and selected image indexes.
    myTreeView.ImageIndex = 0;
    myTreeView.SelectedImageIndex = 1;

    /* Set the index of image from the 
    ImageList for selected and unselected tree nodes.*/
    this.rootImageIndex = 2;
    this.selectedCustomerImageIndex = 3;
    this.unselectedCustomerImageIndex = 4;
    this.selectedOrderImageIndex = 5;
    this.unselectedOrderImageIndex = 6;
    
    // Create the root tree node.
    TreeNode rootNode = new TreeNode("CustomerList");
    rootNode.ImageIndex = rootImageIndex;
    rootNode.SelectedImageIndex = rootImageIndex;
      
    // Add a main root tree node.
    myTreeView.Nodes.Add(rootNode);

    // Add a root tree node for each Customer object in the ArrayList.
    foreach(Customer myCustomer in customerArray)
    {
        // Add a child tree node for each Order object.
        int countIndex=0;
        TreeNode[] myTreeNodeArray = new TreeNode[myCustomer.CustomerOrders.Count];
        foreach(Order myOrder in myCustomer.CustomerOrders)
        {
            // Add the Order tree node to the array.
            myTreeNodeArray[countIndex] = new TreeNode(myOrder.OrderID,
              unselectedOrderImageIndex, selectedOrderImageIndex);
            countIndex++;
        }
        // Add the Customer tree node.
        TreeNode customerNode = new TreeNode(myCustomer.CustomerName,
            unselectedCustomerImageIndex, selectedCustomerImageIndex, myTreeNodeArray);
        myTreeView.Nodes[0].Nodes.Add(customerNode);
    }
}

[C++] 
private:
    void FillTreeView() {
        // Load the images in an ImageList.
        ImageList* myImageList = new ImageList();
        myImageList->Images->Add(Image::FromFile(S"Default.gif"));
        myImageList->Images->Add(Image::FromFile(S"SelectedDefault.gif"));
        myImageList->Images->Add(Image::FromFile(S"Root.gif"));
        myImageList->Images->Add(Image::FromFile(S"UnselectedCustomer.gif"));
        myImageList->Images->Add(Image::FromFile(S"SelectedCustomer.gif"));
        myImageList->Images->Add(Image::FromFile(S"UnselectedOrder.gif"));
        myImageList->Images->Add(Image::FromFile(S"SelectedOrder.gif"));

        // Assign the ImageList to the TreeView.
        myTreeView->ImageList = myImageList;

        // Set the TreeView control's default image and selected image indexes.
        myTreeView->ImageIndex = 0;
        myTreeView->SelectedImageIndex = 1;

        /* Set the index of image from the
        ImageList for selected and unselected tree nodes.*/
        this->rootImageIndex = 2;
        this->selectedCustomerImageIndex = 3;
        this->unselectedCustomerImageIndex = 4;
        this->selectedOrderImageIndex = 5;
        this->unselectedOrderImageIndex = 6;

        // Create the root tree node.
        TreeNode* rootNode = new TreeNode(S"CustomerList");
        rootNode->ImageIndex = rootImageIndex;
        rootNode->SelectedImageIndex = rootImageIndex;

        // Add a main root tree node.
        myTreeView->Nodes->Add(rootNode);

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

            // Add a child tree node for each Order object.
            int countIndex=0;
            TreeNode* myTreeNodeArray[] = new TreeNode*[myCustomer->CustomerOrders->Count];
            IEnumerator* myEnum = myCustomer->CustomerOrders->GetEnumerator();
            while (myEnum->MoveNext()) {
                Order* myOrder = __try_cast<Order*>(myEnum->Current);

                // Add the Order tree node to the array.
                myTreeNodeArray->Item[countIndex] = new TreeNode(myOrder->OrderID,
                    unselectedOrderImageIndex, selectedOrderImageIndex);
                countIndex++;
            }
            // Add the Customer tree node.
            TreeNode* customerNode = new TreeNode(myCustomer->CustomerName,
                unselectedCustomerImageIndex, selectedCustomerImageIndex, myTreeNodeArray);
            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.

See Also

TreeNode Class | TreeNode Members | System.Windows.Forms Namespace

Show:
© 2015 Microsoft