My Treeview Project | Episode Four: Expand All and Collapse All Buttons

In episode 4 we'll take the treeview we designed in episode 3 and add buttons for "collapse all" and "expand all". This will let us get started working with the nodes in a treeview.

In previous episodes I told you to make sure you'd built the treeview from the last episode so we could add to it. This time you can download the completed treeview including this episode. Download the episode 4 database. All we've added this time are the command buttons for expanding and collapsing the nodes. Here's the code for the collapse all button:

Private Sub cmdCollapseAll_Click()
  Dim nodThis As MSComctlLib.Node
  For Each nodThis In Me.xProductTreeview.Nodes ' loop through all nodes
    nodThis.Expanded = False
  Next nodThis
End Sub

There are actually a few different ways to process all the nodes in a treeview. I prefer to use For Each...Next whenever I can. It just seems the most "self documenting". In this case we declare a variable of type Node. (I specified MSComctlLib.Node because it's good practice to be explicit when delcaring variables of object types from libraries outside of Access. I have one application that uses both Word and Excel automation. Both Word and Excel have a Range object. If I don't specify, which one will I get?) This variable will represent one node of the tree. The for loop tells Access to do a block of code once for each member of the collection of nodes in the treeview. Then it's as simple as settting each node's Expanded property to False.

This is a full page view of an article from My MS Access Blog.

The last line just sets focus back to the treeview. I do this because the selected item in a treeview looks different when the tree has focus from when it doesn't. In episode three when we formatted our tree we set the .HideSelection property to False. This means the selected item will still be identified when the tree doesn't have focus, but it still doesn't get the full item highlight, it's more of a dimmed highlight so the user knows it's not in focus. All that being said, the appearance of the tree will be more what the user expects if you set focus back to it after you do something with it.

Here's the expand all button- it's not much different than the collapse all button:

Private Sub cmdExpandAll_Click()
  Dim nodThis As MSComctlLib.Node
  For Each nodThis In Me.xProductTreeview.Nodes ' loop through all nodes
    nodThis.Expanded = True
  Next nodThis
  With Me.xProductTreeview
    .SetFocus ' move focus back to the treeview

    ' make sure the selected item is back into the visible part of the treeview
  End With
End Sub

The loop at the beginning was probably pretty predictable- just like the last one but set the Expanded property to True instead. Then there's a With block at the bottom that does a couple of things to the treeview. I use a with block because I will do two things to the same object. The first line sets focus just like in the previous button. The line .SelectedItem.EnsureVisible makes sure that with all the additional rows we're showing that the selected node still shows up. A treeview's .SelectedItem is the node currently selected, and the .EnsureVisible method scrolls the treeview if necessary and expands nodes as needed to make sure a particular node is visible.

That's it for episode 4. In epsiode 5 we'll make the tree really do something- we'll use it to select and edit records.

Check out all the posts in MyTreeviewProject.