Skip to content

Child nodes

Every node stores all child nodes in the Nodes property. To add or remove a child node, you have two options:

  • Using the Nodes property to add or remove child nodes.
  • Using the childNode.AttachTo(parentNode) method to add a child node to the specified parent.

Example

Node n1 = new Node("1");
Node n2 = new Node("2");
n2.AttachTo(n1);
Node n3 = new Node("3");
n2.Nodes.Add(n3);
Dim n1 As New Node(1)
Dim n2 As New Node(2)
n2.AttachTo(n1)
Dim n3 As New Node(3)
n2.Nodes.Add(n3)

Explore child nodes#

Flexible TreeView provides many properties and methods to count, retrieve, or change child nodes of each parent node.

To check whether a particular parent node has any child nodes, use the HasChildren node property.

To traverse through all child nodes, including child nodes of child nodes, use the AllNodes property, which returns an enumerator of all child nodes of all levels, as shown below.

Node parent = new Node();
Node child1 = new Node();
parent.Nodes.Add(child1);
Node child2 = new Node();
child1.Nodes.Add(child2);

// parent.AllNodes returns child1 and then child2 nodes.
IEnumerable<Node> children = parent.AllNodes;
Dim parent As New Node()
Dim child1 As New Node()
parent.Nodes.Add(child1)
Dim child2 As New Node()
child1.Nodes.Add(child2)

' parent.AllNodes returns child1 and then child2 nodes.
Dim children As IEnumerable(Of Node) = parent.AllNodes

The ExpandedNodes property provides the same list of child nodes as AllNodes does, but it counts only expanded child nodes, i.e., those nodes that have the Expanded property set to true.

Count child nodes#

The ChildrenCount node property returns the count of first-level child nodes stored in the Nodes collection property. To get the count of child nodes on all levels, use the AllChildrenCount property as shown below.

Node parent = new Node();
Node child1 = new Node();
parent.Nodes.Add(child1);
Node child2 = new Node();
child1.Nodes.Add(child2);

// firstLevelChildrenCount = 1 (child1)
int firstLevelChildrenCount = parent.ChildrenCount;

// firstLevelChildrenCount = 2 (child1 and child2)
int allChildrenCount = parent.AllChildrenCount;
Dim parent As New Node()
Dim child1 As New Node()
parent.Nodes.Add(child1)
Dim child2 As New Node()
child1.Nodes.Add(child2)

' firstLevelChildrenCount = 1 (child1)
Dim firstLevelChildrenCount As Integer = parent.ChildrenCount

' firstLevelChildrenCount = 2 (child1 and child2)
Dim allChildrenCount As Integer = parent.AllChildrenCount

To get the count only of the expanded child nodes, use the ExpandedChildrenCount and AllExpandedChildrenCount node properties, which return the count of the first-level and all expanded child nodes, respectively.

Searching for child nodes#

When you need to get node's child nodes by some criteria, use the following methods:

  • Find - allows you to find child nodes by a predicate.
  • FindChildNodeById - allows you to find a single child node by its identifier. To find such a node, it should implement the IIndexable<T> interface. Flexible TreeView does provide the IndexableNode built-in class, which implements the IIndexable<T> interface.
  • FindChildNodeByText - allows you to find a single child node by its Text property. A regex search pattern is supported.
  • FindChildNodesByText - allows you to find multiple child nodes by their Text property. A regex search pattern is supported.
  • FindChildNodeByTag - allows you to find a single child node by its Tag property.
  • FindChildNodesByTag - allows you to find multiple child nodes by their Tag property.

FindChildNodeById#

The FindChildNodeById node method can be used to search for the nodes that have an identifier. To use this method, your node class must implement the IIndexable<T> interface as shown below.

class SearchableNode : Node, IIndexable<int>
{
    public int Id { get; set; }
}
Class SearchableNode
    Inherits Node
    Implements IIndexable(Of Integer)
    Public Property Id() As Integer
        Get
            Return m_Id
        End Get
        Set
            m_Id = Value
        End Set
    End Property
    Private m_Id As Integer
End Class

We suggest selecting the T data type of IIndexable<T> to be fast and small, like int or long. If the node identifier is of long type, the IndexableNode built-in class could be used instead of defining a new node class.

After filling the treeview with nodes, the required node can be found by passing its identifier value into the FindChildNodeById method, as shown below.

Note

Only nodes of SearchableNode type will be considered while iterating the treeview in the sample below.

for (int i = 0; i < 5; i++)
{
    // SearchableNode is defined above.
    Node node = new SearchableNode
    {
        Text = i.ToString(),
        Id = i
    };
    node.AttachTo(tree);
}

Node foundNode = tree.Root.FindChildNodeById<SearchableNode, long>(3);
For i As Integer = 0 To 4
    ' SearchableNode is defined above.
    Dim node As Node = New SearchableNode() With {
        .Text = i.ToString(),
        .Id = i
    }
    node.AttachTo(tree)
Next

Dim foundNode As Node = tree.Root.FindChildNodeById(Of SearchableNode, Long)(3)

The above call of the FindChildNodeById method must be read as 'look for all nodes of SearchableNode type and return the first one that has its Id property equal to 3'.

Search by regular expression#

When using the FindChildNodeByText or FindChildNodesByText methods, a regular expression pattern could be passed as the text parameter to find nodes by a complex pattern. To treat the passed text parameter value as the regular expression, it should start with the ^ character and end with the $ character as shown below.

Node child = parent.FindChildNodeByText<Node>("^.*sometext.*$");
Dim child As Node = parent.FindChildNodeByText(Of Node)("^.*sometext.*$")