Results 1 to 1 of 1

Thread: [VB6] Cascade Expand/Collapse Selected TreeView Node

  1. #1

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    [VB6] Cascade Expand/Collapse Selected TreeView Node

    Simple solution to using the CTRL key to cascade expand/collapse a selected node. By cascading, we are talking about applying the expansion/collapse to every descendant of the selected node. Nothing mind-bending here.

    Simply add a new treeview control to a sample project and paste the following code. If using v5 of the treeview control, replace "MSComctlLib" below with "ComctlLib". Run project and play with expanding/collapsing nodes with/without using the Ctrl key. This should work whether you cascade using the keyboard or mouse and whether you are double clicking on the node or its root lines.
    Code:
    Option Explicit
    
    Private m_CtrlKey As Boolean
    
    Private Sub pvChainExpand(pNode As Node, bExpand As Boolean)
        
        Dim cNode As Node
        If bExpand = False Then pNode.Expanded = bExpand
        If pNode.Children <> 0 Then
            Set cNode = pNode.Child.FirstSibling
            Do
                If cNode.Children Then pvChainExpand cNode, bExpand
                Set cNode = cNode.Next
            Loop Until cNode Is Nothing
        End If
        If bExpand = True Then pNode.Expanded = bExpand
        
    End Sub
    
    Private Sub TreeView1_Collapse(ByVal Node As MSComctlLib.Node)
        If m_CtrlKey = True Then
            m_CtrlKey = False: pvChainExpand Node, False
        End If
    End Sub
    
    Private Sub TreeView1_Expand(ByVal Node As MSComctlLib.Node)
        If m_CtrlKey = True Then
            m_CtrlKey = False: pvChainExpand Node, True
        End If
    End Sub
    
    Private Sub TreeView1_KeyDown(KeyCode As Integer, Shift As Integer)
        If (Shift And vbCtrlMask) <> 0 Then m_CtrlKey = True
    End Sub
    
    Private Sub TreeView1_KeyUp(KeyCode As Integer, Shift As Integer)
        If (Shift And vbCtrlMask) = 0 Then m_CtrlKey = False
    End Sub
    
    Private Sub Form_Load()
        ' Add test nodes
        Dim n As Long, tNode As Node
        
        TreeView1.LineStyle = tvwRootLines   ' show lines so they can be double-clicked on
        TreeView1.Indentation = 20 * Screen.TwipsPerPixelX
        
        Set tNode = TreeView1.Nodes.Add(, , , "Sample Tree")
        Set tNode = TreeView1.Nodes.Add(tNode, tvwChild, , "Leaf Level 1")
        For n = 1 To 5
            TreeView1.Nodes.Add tNode, tvwChild, , "Leaf Level 2"
        Next
        Set tNode = tNode.Child.LastSibling
        For n = 1 To 3
            TreeView1.Nodes.Add tNode, tvwChild, , "Leaf Level 3"
        Next
        Set tNode = tNode.Parent.Parent
        For n = 1 To 2
            TreeView1.Nodes.Add tNode, tvwChild, , "Leaf Level 1"
        Next
        
    End Sub
    This might be a nice option to offer your users. The code can be pretty easily modified. The key routine is the recursive pvChainExpand routine. If wanting to use the ALT or Shift key instead, replace vbCtrlMask with vbAltMask/vbShiftMask respectively.

    Edited for clarity. Hold the CTRL key down while expanding/collapsing
    Last edited by LaVolpe; Sep 22nd, 2018 at 12:59 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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