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
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.
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.
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.
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.