Programatically check parent node independently of child nodes

1 Answer 36 Views
Treeview
Jure
Top achievements
Rank 2
Iron
Iron
Jure asked on 15 Sep 2021, 10:14 AM

Hi.

Is it possible to set the CheckState of a parent node independently of how the child nodes are set? In my code, it won't let me change the state if there are different states in child node.

Here's the code, just a simple form with a RadTreeView named "tree" on it:

 

Imports Telerik.WinControls
Imports Telerik.WinControls.UI

Public Class frmTest
    Private Sub frmTest_Load(sender As Object, e As EventArgs) Handles Me.Load
        Me.tree.CheckBoxes = True
        Me.tree.AutoCheckChildNodes = False
        Me.tree.TriStateMode = True

        For i As Integer = 1 To 2
            Dim node As New RadTreeNode("Parent " + i.ToString)
            Me.tree.Nodes.Add(node)
            For j As Integer = 1 To 3
                node.Nodes.Add("Child " + j.ToString)
            Next
        Next

        Me.tree.ExpandAll()

    End Sub

    Private Sub tree_NodeMouseClick(sender As Object, e As RadTreeViewMouseEventArgs) Handles tree.NodeMouseClick

        e.Node.Current = True
        Select Case e.Node.CheckState

            Case Enumerations.ToggleState.On
                e.Node.CheckState = Enumerations.ToggleState.Off
            Case Enumerations.ToggleState.Off
                e.Node.CheckState = Enumerations.ToggleState.Indeterminate
            Case Enumerations.ToggleState.Indeterminate
                'won't change the state on a parent node if not all child nodes are checked!
                e.Node.CheckState = Enumerations.ToggleState.On
        End Select
        e.Node.Current = False

    End Sub

    Private Sub tree_NodeCheckedChanging(sender As Object, e As RadTreeViewCancelEventArgs) Handles tree.NodeCheckedChanging
        If Not e.Node.Current Then
            e.Cancel = True
        End If
    End Sub
End Class

I hope that makes sense..

1 Answer, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 17 Sep 2021, 01:58 PM
Hello, Jure, 

Note that when you click the checkbox of a node element, it is expected RadTreeView internally to toggle the check state and make the affected node selected/current. When the AutoCheckChildNodes property is set to false, the child nodes are not expected to be affected by the parent node's ToggleState. Indeed, in this case, if you want to a parent node to be toggled (ToggleState.On), ToggleState.Indeterminate will be possible. This is because the TriStateMode property is enabled according to the provided code snippet.

The possible solution that I can suggest in this case is to turn off the AutoCheckChildNodes and the TriStateMode and handle all checks in the NodeCheckedChagned event. Hence, if you want to manage the child nodes in any particular way when the parent node is checked, you can do it in the NodeCheckedChagned event. RadTreeNode offers the Level property which indicates the hierarchy level of the node and thus you can determine whether the root node or a child node is affected.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Jure
Top achievements
Rank 2
Iron
Iron
commented on 17 Sep 2021, 04:28 PM

Thank you Dess! I only had to turn off the TriStateMode to get the result that I wanted. I ignore the checkbox of a node click by canceling in the NodeCheckedChanging event, if it wasn't before clicked on the node text (not here in the test form) … I leave the NodeCheckedChanged event for saving in the DB...
Tags
Treeview
Asked by
Jure
Top achievements
Rank 2
Iron
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or