vb.netrecursiondevexpresstreelistxtratreelist

TreeList - hide nodes when children do not exist


I use the DevExpress Framework for development in VB.net. I am facing issues with the DevExpress TreeList control, which is not a problem with the control itself but the algorithm for hiding specific nodes. I am not able to find a suitable solution.

Problem:

A checkbox fires the event to hide nodes which do not have nodes with the [Type] "GLAccount". The code snipped below works in as much as only the parents of GLAccounts will be hidden but it does not inherits the information upwards (to the grandparents and their parents) to make them invisible as well. As soon as there aren't any nodes with the [Type] "GLAccount" the parents should be not visible, but if sibbling nodes have "GLAccounts" then not all parents should be invisble.

Checkbox event:

Private Sub BarCheckItem1_CheckedChanged_2(sender As Object, e As ItemClickEventArgs) Handles bciHideEmptyAccounts.CheckedChanged
        If CType(bciHideEmptyAccounts.Checked, Boolean) Then
            hideNodesRecursive(treeListLeft.Nodes, False)
        Else
            hideNodesRecursive(treeListLeft.Nodes, True)
        End If
    End Sub

Sub being fired by the Checkbox event depending whether Checkbox value is true/false:

 Private Sub hideNodesRecursive(ByVal nodes As TreeListNodes, ByVal bHide As Boolean)
        For Each node As TreeListNode In nodes
            If node.HasChildren Then
                hideNodesRecursive(node.Nodes, bHide)
            Else
                If Not CType(node.GetValue("Type"), String) = "GLAccount" Then
                    node.Visible = bHide
                End If
            End If
        Next
    End Sub

Use Case 1: Structure given shows different level of nodes. GLAccounts do only occure in the last level (they do not have childs). The blue highlighting shows that the event hides the nodes properly. enter image description here

Result: Previously blue highlighted node disappeared. enter image description here

Uase Case 2: again, blue highligthed nodes will be properly hidden, but the orange node is still visible. enter image description here

Result: enter image description here


Solution

  • The visibility of each node can be evaluated by the logical OR of all its Childs. So, even one of its Childs is visible (has a value of "GLAccount") it should be visible.

    I slightly changed the BarCheckItem1_CheckedChanged routine. Also added a simple sub to show all nodes (Show_Nodes) because cannot found any method in DevExpress documentation for setting the visibilty of all treelist nodes.

    Can not check the code by the time of this post, but it is logically correct.

    Private Sub BarCheckItem1_CheckedChanged(sender As Object, e As EventArgs) Handles bciHideEmptyAccounts.CheckedChanged
        treeListLeft.BeginUpdate()
        IF bciHideEmptyAccounts.Checked THEN
            hideNodesRecursive(TreeListLeft.Nodes)
        ELSE
            Show_Nodes(TreeListLeft.nodes)
        END IF
        treeListLeft.EndUpdate()
    End Sub
    
    
    Private Sub hideNodesRecursive(ByVal nodes As TreeListNodes)
        For Each node As TreeListNode In nodes
            node.visible= CSTR(node.GetValue("Type")).equals("GLAccount")
            IF NOT isnothing(node.parentnode) THEN
                node.parentnode.visible= (node.parentnode.visible OR node.visible)
            END IF
            hideNodesRecursive(node.Nodes)
        Next
    End Sub
    
    
    SUB Show_Nodes(nodes AS treelistnodes)
        FOR EACH node AS treelistnode IN nodes
            node.visible= TRUE
            Show_Nodes(node. Nodes)
        NEXT
    END SUB
    

    EDIT: treeListLeft.BeginUpdate() / treeListLeft.EndUpdate() added according to the point mentioned in comment by smartini