VB icon

Auto Check Treeview

Email
Submitted on: 1/26/2015 8:26:00 AM
By: Wolfgang Schoenberger (from psc cd)  
Level: Intermediate
User Rating: By 10 Users
Compatibility: VB 5.0, VB 6.0
Views: 760
 
     This handy little procedure will handle all child Checkboxes in a TreeView control. If you check a parent node it will automatically check the child node(s). The procedure is called recursivly.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Auto Check Treeview
' Description:This handy little procedure will handle all child Checkboxes in a TreeView control. If you check a parent node it will automatically check the child node(s).
The procedure is called recursivly.
' By: Wolfgang Schoenberger (from psc cd)
'**************************************

' Add this procedure to your source code
Private Sub process_check(node As node, ch As Boolean, frst As Boolean)
' ch = True or False, depending of the first node
' frst is True when the procedure is called for the first time
' otherwise frst is always false
Dim n As node
Dim n2 As node
' If the current Node has no children and procedure is called 1st time
' just check the node and exit
If node.Children = 0 And frst Then
node.Checked = True
Exit Sub
End If
Set n2 = node
While Not n2 Is Nothing
' If the node has children
' check the node and call process_check recursivly with the first child node, ch
' and False as frst parameter
If n2.Children Then
n2.Checked = ch
process_check n2.Child, ch, False
' If procedure is called 1st time, set n2 to Nothing, so that Loop can end
' otherwise set n2 to the next sibling node
If frst Then
Set n2 = Nothing
Else
Set n2 = n2.Next
End If
' If node has no children, check the node and set n2 to the next sibling node
Else
n2.Checked = ch
Set n2 = n2.Next
End If
Wend
End Sub
' You can call this procedure from your node_check event.
' Exmaple
Private Sub TV1_NodeCheck(ByVal node As MSComctlLib.node)
process_check node, node.Checked, True
End Sub


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.