VCL Delphi

Title: A Gardener's Guide To The Tree View: Part I
Question: This is the first in a set of tutorials to be used as reference for manipulation of the TTreeView class in Delphi. In addition to article text, I will include full project source that will make use of the topics discussed in each part of the tutorial.
Answer:
First off, I'd like to go briefly over what a TreeView is and it's potential uses in your application. For those that aren't familiar with the control, you can very quickly see an example of it by opening your Explorer window (not Internet Explorer, mind you). The left side of Explorer displays a TreeView starting with the Desktop node as the root. All TreeView components store hierarchal sets of data, that can have their children collapsed (like your C drive is by default in Explorer), or Expanded (like My Computer is by default).
In your own applications, a TreeView can be used in most cases where you are displaying hierarchal data. Some simple extensions to the component can yield even greater abilities, such as multiple selection, though we'll save the gory details of this approach for a future tutorial.
A TreeView, beyond the general properties you would expect from any decendant of TWinControl, contains a list of TTreeNode objects. In order to manage the hierarchy properly, the TTreeNode class defines a property for its parent, among other things. Conceptually, this approach is rather similar to a discussion board's postings as they would be saved in the database. If there is sufficient demand for a further description of hierarchal data storage, then I will provide a tutorial focusing on the subject.
This first tutorial will go through the steps of adding root and child nodes. Because I am not a firm believer in tunnel vision, I have approached this tutorial in such a way that it goes over some concepts that are not limited to the scope of a TreeView. Typecasting and Pointers are covered lightly, as they are truly necessary for extending the TreeView, as well as many other classes, to their fullest extent.
The two methods of the TTreeNodes (the collection of TTreeNode objects) that you need to concern yourself with are "Add" and "AddChild". The former is used for adding root-level nodes, or for adding a node at the same level as another. When I speak of level, I am referring to the depth of the node within the tree view. For example, consider that we have the following information stored in a tree view:
Shapes
Cornered
Triangle
Square
Round
Circle
Oval
Shapes is a level 1 node, or a "root-level" node. Cornered and Round are both level 2 nodes, each having the "Shapes" node as a parent. Triangle and Square are level 3 nodes, having the "Cornered" node as a parent.
I'm going to go briefly over the declaration of the "Add" and "AddChild" methods. For a more detailed explanation, please refer to the component URL. I have provided a full Delphi 4 project that makes use of these methods, as well as extensive commenting for each line of processing to better explain to you what's going on and why.
function Add(Node: TTreeNode; const S: string): TTreeNode;
The Add function is used to add a same-level node, or to add a root node. The first parameter is a pointer to a node that the new node will be created as a sibling of. For example, if we had the following TreeNodes already created:
Shapes
Cornered
Triangle
Square
We could add a new item to the same level as the "Triangle" and "Square" nodes by passing a pointer to one or the other to the Add function.
The second parameter is the caption of the new TreeNode object. Once this function has physically created the new node in the TreeView, it returns a pointer to the new node.
The second method we'll be going over is "AddChild", and has the following declaration:
function AddChild(Node: TTreeNode; const S: string): TTreeNode;
This is similar to the Add method, except that the first parameter specifies the parent of the new node. To take our above example, using the AddChild method and passing a pointer to the Shapes node would create a new node at the same level as Cornered.
I hope that this explanation, coupled with the attached project, is of use to Delphi developers. If the feedback on this tutorial is positive, then I will post additional tutorials going into the murky depths and power the the TreeView control.