TreeNode Constructor (String, TreeNode())
Initializes a new instance of the TreeNode class with the specified label text and child tree nodes.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
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 that contains Customer objects.
Public Class Customer Public CustomerOrders As ArrayList Public CustomerName As String Public Sub New(myName As String) CustomerName = myName CustomerOrders = New ArrayList() End Sub 'New End Class 'Customer Public Class Order Public OrderID As String Public Sub New(myOrderID As String) Me.OrderID = myOrderID End Sub 'New End Class 'Order 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
Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.