The demo code which accompanies this article shows how to add items onto a Delphi TreeView, how to associate nodes on the Treeview with icons (or you could use bitmap images etc), and how to trace a path from a selected node to the root. The icon code adds to the complexity of the unit, and has already been discussed on previous pages on this site (follow the link to the Index below).The recursive disk function was also previously used to insert values onto a binary tree, and is recycled here and this time is being used to insert values onto a visual Treeview structure. If you are simply interested in learning to use a TreeView structure you can ignore the graphics code in which case the important procedures and functions are Button1Click, which initializes the root of the TreeView, and then calls FileLook (which scans the disk and subdirectories for folders and files) and the function TracePath, which iteratively moves backwards down the tree and reconstructs a path to the root, in this case returning a string which is a full path to the selected directory or file.
The basics of using a TreeView can be briefly described by the following code fragments...
Initialize the RootNode of the TreeView :
TreeView1.Items.Clear; {clear the Treeview if required}
The syntax for creating the root node of the Tree is:
RootNode := TreeView1.Items.Add(nil, TheTextCaption); {top parent of treeview}
TreeViews get their graphics from an ImageList which is associated with the TreeView using its image property on the VCL property editor. In this example code the program begins by launching the standard Windows Browse for Folder function to find a starting directory for the disk scan so the root is assumed to be a folder. TreeView images can have two views, one when not selected and one when selected. Constants are defined in the code which represent the position of the images within the ImageList. At program start, in the FormCreate event handler the ImageList is loaded with 15 standard icons from Shell32.dll (the code is recycled and only the two folders are actually used in this project.) You will notice a constant string declared with a bunch of numbers encoded. These two digit numbers are extracted from the string and converted to integers and represent the icon position within the Shell32.dll for the icons to be extracted and then added onto the ImageList. To assign a picture from the image list to a TreeView node, you put the integer value of the position of the image on the imagelist into the two image index properties of that node. In this example constant values represented by strings are used.
RootNode.ImageIndex := ISAClosedFolder; {how the image looks to start}
RootNode.SelectedIndex := ISAOpenFolder; {image when item selected}
Any node can then have a child node added onto it using the code:
TempNode := TreeView1.Items.AddChild(Node, CaptionText);
In this example 'TempNode' holds the pointer to the newly created child node of 'Node' and it will have the Caption contained in the string 'CaptionText'. One could then assign TempNode a graphic from the ImageList using the syntax above. Basically the syntax for the root node above and the syntax for creating a child node just illustrated is all you need to know to create a TreeView structure.
Trees are by their nature 'recursive structures'. When a function uses recursion, it means that the function calls itself, rather than calling another function. If you need to read some source code discussing the recursive disk function that loads the Treeview in this example, follow the link to the text file disk_search_recursive.txt. This code, along with the Icon code in this example is simply recycled for this project.
As an example of a recursive function consider the following simple example.
procedure TForm1.Button1Click(Sender: TObject);
var i : integer;
begin
i :=1;
i := AddUpI(i);
ShowMessage('The end result : ' + IntToStr(i));
end;
function TForm1.AddUpI(i : integer):integer;
begin
Result := 0; ShowMessage('Entering function - i = ' + IntToStr(i));
i := i + 1;
If i < 6 then
Result := AddUpI(i);
ShowMessage('Leaving function - Result = ' + IntToStr(Result));
end;
If you are going to experiment with recursion in code, you should save all your work before you test the code, as a fail safe against locking up and crashing your computer. With that being said, the recursive function call where AddUpI calls itself is embedded in the second last line of code. I enters the function set to 1 and Result is first initialized to zero (otherwise anything could be in it). I is incremented. The all important stop condition (if i is less than 6) prevents infinite recursion and a system lock up. As long as i is less than 6 the function calls itself again, passing the value of i. The end result is 15 which is 1 + 2 + 3 + 4 + 5. When i becomes 6 is doesn't get added onto the running total because the recursive function call fails the comparison test in the if statement at this point. When i reaches six rather than calling the function recursively one more time, the code drops to the next line, and outputs a message showing the Result, which the first time this code gets reached (when i reaches 6) is Result = 5. If you watch the message boxes you will see that first i climbs up to 6, and then sequentially all the recursive function calls return and the result begins to climb up to 15, the final total.
Recursion works well for crawling through a disk for all files. What happens is that when a file is encountered the routine continues iterating through a loop, but when a folder is encountered it calls itself recursively passing the folder name. Each instance of a recursive procedural or function call is unique. If i = 5 in one instance of the function, that is the value that it has only for that called instance of the function. Meanwhile the function where i = 4 still exists and is only on hold until the i = 5 function returns. When procedures or functions return they always drop down to the next line of code. So then when a recursive call is made in the Treeview program when a folder is encountered a new instance of the code is created and in this instance the files are scanned that belong in that folder. The files are also attached to the correct parent node on the tree because in that instance of the recursive call, the node is passed as the Parent node (while in the calling instance it was a child). When the code returns from the recursive call, the Node goes back to being a child, and the other recursive call where it was the parent is destroyed and disappears. Furthermore, any other files that still exist in the parent folder are then interatively looped through and inserted as siblings along side the folder which just returned from its recursive call as the parent of its own instance of the code. So the important thing to keep in mind about recursive procedures and functions is that in each instance of the function the values are unique to that instance of the code. This is what makes recursion work. Many programmers are comfortable with iterative code (for loops, while loops, repeat statements) but Trees are naturally recursive, as the disk scanning code demonstrates, and so although it is more difficult to design code using recursion, its worth learning for that very reason.
A few words about the code. The recursive function that scans the drive is called FileLook and it is called by passing it a starting path and wildcards to indicate any file or folder, as well as the root node of the tree, initilized on the TreeView by the Button1Click event handler. In the first recursion the RootNode will be parent node, as described above, and ultimately it will be the parent of every other node on the Treeview.
FileLook(ThePath + '\' + '*.*', RootNode)
The initial path is obtained using a standard Windows API call for a Folder Browser demonstrated in the function BrowseForFolder. The Form Create method loads 15 icons onto the ImageList that will be connected to the TreeView using its Image property on the Object Inspector. It also calls a Windows function that locates the Windows system directory which is where Shell32.dll is located. There are about 80 icons in Shell32.Dll and if you follow the links on the main index page to one of the Icon browsing examples you can open the Shell, browse the icons, and make a note of the number associated with an icon you might want to call for code of your own. For the purposes of this demo the icon numbers are encoded as two numerals in a string constant and are referenced for adding to the TreeNode by a list of constants you will notice in the variable declaration section of the code. There are two sections of Code that find icons for the TreeView, FindFilesIcon and RegistryIconExtraction. Icons can be located in an exe file, an ico icon file, in which case the icon should be extracted from those files. Certain file extensions are associated with certain icons (for example the extension txt is associated with the icon for a text file, and so on). These associations are stored in the registry. A text file does not contain an icon, but rather the icon associated with a text file is stored in Shell32.dll, and this information is found in the registry key for that file extension. The function TracePath demonstrates how to work back to the root from any given selected TreeNode, and in this code it returns a string which is a fully qualified path, including a filename if it was a file that was selected. The source code is commented and hopefully together with the discussion included on this page, will be easy to understand.
A final note. Processing graphics is time intensive even on a fast computer, so you should scan just a few directories and sub folders. Normally what you would want to do is to exclude subdirectories in your disk scan and just scan the current folder, thus quickly filling one branch of the tree at a time. However, to demosntrate recursion, this code scans sub folders, and thus it can seem that perhaps the program has crashed and is not responding, but actually it is just chewing up CPU cycles on all those TreeView graphics. Scan just a few folders and sub folders to get a feel for how the code works.
You can download the source as a text file ... treeview_delphi3.txt the Delphi 3 version or ... treeview_delphi5.txt the Delphi 5 version. The code is also downloadable as a ready to run project file for Delphi 3/Delphi 5 in the following zip file ... treeview_disk_scan.zip 20 KB.
Related pages can be found on the Main index page on this site...
A Unified Field Theory
![]()
The Unified Field Theory
is also available as a zip file -> unified.zip
Introduction :The Pioneer Effect and the New Physics. A brief description of the new physics required to explain the 'Pioneer Effect', which is the constant deceleration of space craft as they fly through space.

