VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Auto Check/UnCheck TreeView

by kc7dji (1 Submission)
Category: Custom Controls/Forms/Menus
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (8 Votes)

This basic procedure will handle all parent and child Checkboxes in a TreeView control. If you check a child node it will automatically check the parent(s) and vise versa.

Inputs
This procedure requires you to pass in the Treeview and the current node.
Assumes
Basic TreeView(s). This code assumes you already have a TreeView populated with "CheckBoxes" turned ON.

Rate Auto Check/UnCheck TreeView


'Put this call into the TreeViews NODECHECK procedure.
Private Sub MyTreeView_NodeCheck(ByVal Node As MSComctlLib.Node)
Call TreeCheckBoxes(MyTreeView, Node)
end Sub

'Add this procedure to a Module or to the form the TreeView is contained.
Public Sub TreeCheckBoxes(TR As TreeView, CurrentNode As Node)
'This code is copyright (c)2002 by Scott Durrett - All Rights Reserved
'No changes are allow without written approval from the Author.

Dim liNodeIndex As Integer
Dim lbDirty As Boolean
Dim liCounter As Integer
Dim lParentNode As Node
Dim lChildNode As Node
lbDirty = False
liNodeIndex = CurrentNode.Index
If CurrentNode.Checked = True Then 'node is checked
  'Children Check/UnCheck
  If Not TR.Nodes.Item(CurrentNode.Index).Child Is Nothing Then
    Set lParentNode = TR.Nodes.Item(liNodeIndex).Child.FirstSibling
      Do While Not lParentNode Is Nothing
    
        lParentNode.Checked = CurrentNode.Checked
        
        If Not lParentNode.Child Is Nothing Then
          Set lChildNode = lParentNode.Child
            Do While Not lChildNode Is Nothing
              lChildNode.Checked = CurrentNode.Checked
                If Not lChildNode.Next Is Nothing Then
                  Set lChildNode = lChildNode.Next
                Else
                  Set lChildNode = lChildNode.Child
                End If
            Loop
        End If
        Set lParentNode = lParentNode.Next
      Loop
  End If
  '============================================================
  'Check all parent nodes
  Do While Not TR.Nodes.Item(liNodeIndex).Parent Is Nothing
    TR.Nodes.Item(liNodeIndex).Parent.Checked = CurrentNode.Checked
    liNodeIndex = TR.Nodes.Item(liNodeIndex).Parent.Index
  Loop
  '===========================

ElseIf CurrentNode.Checked = False Then 'node is unchecked
  'Children Check/UnCheck
  If Not TR.Nodes.Item(CurrentNode.Index).Child Is Nothing Then
    Set lParentNode = TR.Nodes.Item(liNodeIndex).Child.FirstSibling
      Do While Not lParentNode Is Nothing
    
        lParentNode.Checked = CurrentNode.Checked
        
        If Not lParentNode.Child Is Nothing Then
          Set lChildNode = lParentNode.Child
            Do While Not lChildNode Is Nothing
              lChildNode.Checked = CurrentNode.Checked
                If Not lChildNode.Next Is Nothing Then
                  Set lChildNode = lChildNode.Next
                Else
                  Set lChildNode = lChildNode.Child
                End If
            Loop
        End If
      
        Set lParentNode = lParentNode.Next
      Loop
  End If
  '============================================================
Set lParentNode = Nothing
Set lChildNode = Nothing
  If Not CurrentNode.Parent Is Nothing Then
    Set lParentNode = CurrentNode.Parent.Child
      Do While Not lParentNode Is Nothing
        Set lChildNode = lParentNode.FirstSibling
          Do While Not lChildNode Is Nothing
              
            If lChildNode.Checked = True Then
              lbDirty = True
              Exit Do
            End If
            
            'If Not lChildNode.Next Is Nothing Then
              Set lChildNode = lChildNode.Next
            'End If
          Loop
          
          
          If lbDirty = False Then
            If Not lParentNode.Parent Is Nothing Then
              lParentNode.Parent.Checked = False
              lbDirty = False
            End If
          Else
            Exit Do
          End If
          
      
      If Not lParentNode.Parent Is Nothing Then
        Set lParentNode = lParentNode.Parent
      Else
        Set lParentNode = lParentNode.Parent
      End If
    Loop
  End If
      
End If

Set CurrentNode = Nothing
Set lParentNode = Nothing
Set lChildNode = Nothing

End Sub
'The End

Download this snippet    Add to My Saved Code

Auto Check/UnCheck TreeView Comments

No comments have been posted about Auto Check/UnCheck TreeView. Why not be the first to post a comment about Auto Check/UnCheck TreeView.

Post your comment

Subject:
Message:
0/1000 characters