Back to Devexpress

How to: Select a node's children

windowsforms-5679-controls-and-libraries-tree-list-examples-focus-and-selection-how-to-select-a-nodes-children.md

latest1.9 KB
Original Source

How to: Select a node's children

  • Aug 01, 2019

The following code shows how to select children of a specific node. In the example, nodes are selected via the TreeListMultiSelection.Set method of the TreeList.Selection object.

The image below shows the result of selecting a Sales and Marketing node’s children.

csharp
using DevExpress.XtraTreeList.Nodes;

TreeListNode node = treeList1.FindNodeByFieldValue("Department", "Sales and Marketing");
if (node != null) {
    ArrayList selectedNodes = new ArrayList();
    selectChildren(node, selectedNodes);
    treeList1.Selection.Set(selectedNodes);
}

void selectChildren(TreeListNode parent, ArrayList selectedNodes) {
   IEnumerator en = parent.Nodes.GetEnumerator();
   TreeListNode child;
   while(en.MoveNext()) {
      child = (TreeListNode)en.Current;
      selectedNodes.Add(child);
      if(child.HasChildren) selectChildren(child, selectedNodes);
   }
}
vb
Imports DevExpress.XtraTreeList.Nodes

Dim node As TreeListNode = treeList1.FindNodeByFieldValue("Department", "Sales and Marketing")
If node IsNot Nothing Then
    Dim selectedNodes As New ArrayList()
    selectChildren(node, selectedNodes)
    treeList1.Selection.[Set](selectedNodes)
End If

Sub SelectChildren(ByVal parent As TreeListNode, ByVal selectedNodes As ArrayList)
   Dim en As IEnumerator = parent.Nodes.GetEnumerator()
   Dim child As TreeListNode
   While en.MoveNext() = True
      child = CType(en.Current, TreeListNode)
      selectedNodes.Add(child)
      If child.HasChildren = True Then
         selectChildren(child, selectedNodes)
      End If
   End While
End Sub