none
Treeview Control - chek child if parent is checked RRS feed

  • Question

  • Hi all,
    please can someone suggest me a way for automatically check child node in a treeview control when the parent node is checked.
    In particular, How can I intercept the event of check in the parent node?
    Regards,
    Giovanni
    Tuesday, February 9, 2010 11:19 AM

Answers

  • Hi Giovanni,


          this code will used to you. this code child will ture then parent will check it automatically




        Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck
            'Dim parent_node As TreeNode = e.Node
            'Dim is_checked As Boolean = parent_node.Checked
            'For i As Integer = 0 To e.Node.Nodes.Count - 1
            '    SetSubtreeChecked(parent_node.Nodes(i), is_checked)
            'Next i
            Dim blnUncheck As Boolean = False
            'Check to see if a parent node exists.

            If Not e.Node.Parent Is Nothing Then
                'Loop through the child nodes.
                For Each child As TreeNode In e.Node.Parent.Nodes
                    'Check to see if the current node is unchecked.
                    If child.Checked = True Then
                        'Set the variable.
                        blnUncheck = True
                    End If
                Next
                'Check the variable.
                If blnUncheck = False Then
                    'Check the parent node.
                    e.Node.Parent.Checked = False
                Else
                    'Uncheck the parent node.
                    e.Node.Parent.Checked = True
                End If
            End If
        End Sub
        ''========================================================
        ''          For Tree view Items Validation
        ''========================================================
        Private Sub SetSubtreeChecked(ByVal parent_node As TreeNode, ByVal is_checked As Boolean)
            ' Set the parent's Checked value.
            parent_node.Checked = is_checked
            ' Set the child nodes' Checked values.
            For i As Integer = 0 To parent_node.Nodes.Count - 1
                SetSubtreeChecked(parent_node.Nodes(i), is_checked)
            Next i
        End Sub
    Tuesday, February 9, 2010 12:25 PM

All replies

  • Hi Giovanni,


          this code will used to you. this code child will ture then parent will check it automatically




        Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck
            'Dim parent_node As TreeNode = e.Node
            'Dim is_checked As Boolean = parent_node.Checked
            'For i As Integer = 0 To e.Node.Nodes.Count - 1
            '    SetSubtreeChecked(parent_node.Nodes(i), is_checked)
            'Next i
            Dim blnUncheck As Boolean = False
            'Check to see if a parent node exists.

            If Not e.Node.Parent Is Nothing Then
                'Loop through the child nodes.
                For Each child As TreeNode In e.Node.Parent.Nodes
                    'Check to see if the current node is unchecked.
                    If child.Checked = True Then
                        'Set the variable.
                        blnUncheck = True
                    End If
                Next
                'Check the variable.
                If blnUncheck = False Then
                    'Check the parent node.
                    e.Node.Parent.Checked = False
                Else
                    'Uncheck the parent node.
                    e.Node.Parent.Checked = True
                End If
            End If
        End Sub
        ''========================================================
        ''          For Tree view Items Validation
        ''========================================================
        Private Sub SetSubtreeChecked(ByVal parent_node As TreeNode, ByVal is_checked As Boolean)
            ' Set the parent's Checked value.
            parent_node.Checked = is_checked
            ' Set the child nodes' Checked values.
            For i As Integer = 0 To parent_node.Nodes.Count - 1
                SetSubtreeChecked(parent_node.Nodes(i), is_checked)
            Next i
        End Sub
    Tuesday, February 9, 2010 12:25 PM
  • Hi,
    Really thanks for your usefull and clear reply.
    I tried to use your code and it works perfectly in my application 8and above all it's really clear and understandable).
    Giovanni

    Thursday, February 11, 2010 10:44 PM