Results 1 to 5 of 5

Thread: Checking & UnChecking in TreeView Control

  1. #1

    Thread Starter
    Fanatic Member 007shahid's Avatar
    Join Date
    Feb 2001
    Posts
    562

    Checking & UnChecking in TreeView Control

    I have set the options for Check boxes in my TreeView Control (MS Windows Common Controls). In the NodeCheck event of the control, I want to write a code in such a way that; when a Node is Un/checked all child nodes of that node if any should also have the same checked value.
    Can anyone please help me on this problem
    THE TIME/WEATHER IS
    Don't know how to use APIs or have problem with them,
    Download API-Guide & API-Viewer from http://www.allapi.net

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    VB Code:
    1. 'from Planet Source Code
    2.  
    3.  
    4. Public Sub TreeCheckBoxes(TR As TreeView, CurrentNode As Node)
    5. Dim lbDirty As Boolean, liCounter As Integer
    6. Dim lParentNode As Node, lChildNode As Node
    7.    
    8.   lbDirty = False
    9.   liNodeIndex = CurrentNode.Index
    10.  
    11.   If CurrentNode.Checked = True Then
    12.     If Not TR.Nodes.Item(CurrentNode.Index).Child Is Nothing Then
    13.      
    14.       Set lParentNode = TR.Nodes.Item(CurrentNode.Index).Child.FirstSibling
    15.  
    16.  
    17.             Do While Not lParentNode Is Nothing
    18.                 lParentNode.Checked = CurrentNode.Checked
    19.  
    20.  
    21.                 If Not lParentNode.Child Is Nothing Then
    22.                     Set lChildNode = lParentNode.Child
    23.  
    24.  
    25.                     Do While Not lChildNode Is Nothing
    26.                         lChildNode.Checked = CurrentNode.Checked
    27.  
    28.  
    29.                         If Not lChildNode.Next Is Nothing Then
    30.                             Set lChildNode = lChildNode.Next
    31.                         Else
    32.                             Set lChildNode = lChildNode.Child
    33.                         End If
    34.                     Loop
    35.                 End If
    36.                 Set lParentNode = lParentNode.Next
    37.             Loop
    38.         End If
    39.  
    40.     ElseIf CurrentNode.Checked = False Then 'node is unchecked
    41.  
    42.  
    43.         If Not TR.Nodes.Item(CurrentNode.Index).Child Is Nothing Then
    44.             Set lParentNode = TR.Nodes.Item(CurrentNode.Index).Child.FirstSibling
    45.  
    46.  
    47.             Do While Not lParentNode Is Nothing
    48.                 lParentNode.Checked = CurrentNode.Checked
    49.  
    50.  
    51.                 If Not lParentNode.Child Is Nothing Then
    52.                     Set lChildNode = lParentNode.Child
    53.  
    54.  
    55.                     Do While Not lChildNode Is Nothing
    56.                         lChildNode.Checked = CurrentNode.Checked
    57.  
    58.  
    59.                         If Not lChildNode.Next Is Nothing Then
    60.                             Set lChildNode = lChildNode.Next
    61.                         Else
    62.                             Set lChildNode = lChildNode.Child
    63.                         End If
    64.                     Loop
    65.                 End If
    66.                 Set lParentNode = lParentNode.Next
    67.             Loop
    68.         End If
    69.  
    70.         Set lParentNode = Nothing
    71.         Set lChildNode = Nothing
    72.  
    73.  
    74.         If Not CurrentNode.Parent Is Nothing Then
    75.             Set lParentNode = CurrentNode.Parent.Child
    76.  
    77.  
    78.             Do While Not lParentNode Is Nothing
    79.                 Set lChildNode = lParentNode.FirstSibling
    80.  
    81.  
    82.                 Do While Not lChildNode Is Nothing
    83.  
    84.  
    85.                     If lChildNode.Checked = True Then
    86.                         lbDirty = True
    87.                         Exit Do
    88.                     End If
    89.  
    90.                     Set lChildNode = lChildNode.Next
    91.                 Loop
    92.  
    93.                 If Not lParentNode.Parent Is Nothing Then
    94.                     Set lParentNode = lParentNode.Parent
    95.                 Else
    96.                     Set lParentNode = lParentNode.Parent
    97.                 End If
    98.             Loop
    99.         End If
    100.     End If
    101.     Set CurrentNode = Nothing
    102.     Set lParentNode = Nothing
    103.     Set lChildNode = Nothing
    104. End Sub
    Last edited by The Hobo; Feb 1st, 2002 at 11:16 AM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Fanatic Member 007shahid's Avatar
    Join Date
    Feb 2001
    Posts
    562
    thanks
    THE TIME/WEATHER IS
    Don't know how to use APIs or have problem with them,
    Download API-Guide & API-Viewer from http://www.allapi.net

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I just implimented this into my Custom TreeView Control, and realized that you could shorten it almost 50% and get the same functionality (is that a word?):

    VB Code:
    1. Public Sub TreeCheckBoxes(TR As TreeView, CurrentNode As Node)
    2. Dim lbDirty As Boolean, liCounter As Integer
    3. Dim lParentNode As Node, lChildNode As Node
    4.    
    5.   lbDirty = False
    6.   liNodeIndex = CurrentNode.Index
    7.  
    8.     If Not TR.Nodes.Item(CurrentNode.Index).Child Is Nothing Then
    9.      
    10.       Set lParentNode = TR.Nodes.Item(CurrentNode.Index).Child.FirstSibling
    11.  
    12.  
    13.             Do While Not lParentNode Is Nothing
    14.                 lParentNode.Checked = CurrentNode.Checked
    15.  
    16.  
    17.                 If Not lParentNode.Child Is Nothing Then
    18.                     Set lChildNode = lParentNode.Child
    19.  
    20.  
    21.                     Do While Not lChildNode Is Nothing
    22.                         lChildNode.Checked = CurrentNode.Checked
    23.  
    24.  
    25.                         If Not lChildNode.Next Is Nothing Then
    26.                             Set lChildNode = lChildNode.Next
    27.                         Else
    28.                             Set lChildNode = lChildNode.Child
    29.                         End If
    30.                     Loop
    31.                 End If
    32.                 Set lParentNode = lParentNode.Next
    33.             Loop
    34.         End If
    35.  
    36.     Set CurrentNode = Nothing
    37.     Set lParentNode = Nothing
    38.     Set lChildNode = Nothing
    39. End Sub
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    This was quite some time ago, but I just wrote up an even better snippet for this on my site:

    VB Code:
    1. Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
    2. Dim iChild As Integer, nodX As Node
    3.  
    4.   If Node.Children <> 0 Then 'if node has children
    5.     Set nodX = Node.Child
    6.     For iChild = 1 To Node.Children
    7.       nodX.Checked = Node.Checked
    8.       Set nodX = nodX.Next
    9.     Next
    10.   End If
    11. End Sub

    Simple and compact. http://www.vbshelf.com/snippets.php?...=controls&id=7
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width