Article ID:qGEN018
Date Revised:November 12, 2000
Keywords:TreeView, ImageList, ActiveX
Synopsis:Sample TreeView form
See Also:treeview101.zip  

Question: How can I use the TreeView control on a form?

Answer: The TreeView control allows you to display hierarchical data, like the Folders window in Windows Explorer.

The above zip file contains a well commented example of displaying the C: directory within the Microsoft TreeView control. It also demonstrates how to use an ImageList control to display graphics in the TreeView. There is a sample in the solutions.app that ships with VFP, but there is not a single comment in that overly complex code.

Also forget about using the Help that is available for the control from it's setup dialog. It has several holes and is pretty much not navigable. A better source of help is the MSDN in the Hardcode Visual Basic book and the Periodicals section. Search for treeview on the MSDN CD or website. There is a topic named TreeView Control Constants that explains the values of some of the method parameters.

The form also has some controls that let you play with the appearance of the TreeView at runtime.

This example was intended to be kept very simple, so there are some missing "bells and whistles".

1) Directory and filenames are all uppercased because the native ADIR() function is used. The WindowsScriptingHost or API calls could be used to retrieve proper cased names.

2) Tree branches do not display a + beside them until that node is selected and subdirectories exist. This works the same as Windows Explorer when looking at a network drive. This infomation could be collected by recursing one level deeper in the DirectoryToTreeNode() method, but that over complicates this simple example.


1