TreeView Control

If you have studied the ListView control you will find the TreeView similar and even more flexible and powerful. The TreeView control provides a 'tree' view of data, identical in scope to Windows Explorer, with nodes and sub-nodes and icons to represent collapsed and expanded nodes. The default icons are the '+' and '-' symbols, which should be adequate for many purposes. 

You have probably come across the idea of binary trees and graphical trees defined recursively in Logo. Well, trees in a TreeView are not like these, they use similar terminology and have a branching structure but they are not balanced in construction or appearance. Instead they are more linear in appearance with any number of items at any level. Nevertheless, they are tree-like and they have their uses for data of a branching nature.

As with the ListView the control provides an editor for setting up trees for viewing at run-time (right-click the control in the form):

The TreeView has file load and save operations so an application can be created with this functionality. A file for a TreeView can be a simple text file with tabs for indents within it:

The Special Magic of Ella and Louis
    Don't Be That Way
        Goodman-Parish-Sampson
    They All Laughed
         Gershwin
    Autumn in New York
         Duke
    Stompin At The Savoy
         Goodman-Webb-Sampson
    I Won't Dance
        Kern-Fields-McHugh-Harbach-Hammerstein II
Getz and Gilberto...

Functionality similar to the ListView example has been built into this application:

An ImageList has been added 

The code for the Add Node button is:

procedure TForm1.Button1Click(Sender: TObject);
begin
 treeview1.Items.add(nil, edit1.text)
end;

The code for the Add SubNode button is:

procedure TForm1.Button2Click(Sender: TObject);
var node:TTreenode;
begin
 node := treeview1.Selected;
 if node <>nil then 
  Treeview1.items.addchild(node, edit2.Text)
 else showmessage ('No node is selected');
end;

The code for the Delete Node button is:

procedure TForm1.Button3Click(Sender: TObject);
var node:TTreenode;
begin
 node:=treeview1.Selected;
 if node <> nil then
  treeview1.Items.Delete(node)
 else
  showmessage ('No node is selected');
end;

The code to open a file for a TreeView is more straightforward than that for a ListView:

procedure TForm1.Open1Click(Sender: TObject);
begin
 if opendialog1.execute then
  TreeView1.LoadFromFile(opendialog1.FileName);
end;

(But note that this does not include image information.) The code for the Save routine:

procedure TForm1.Save1Click(Sender: TObject);
begin
 if savedialog1.execute then
  treeview1.SaveToFile(savedialog1.FileName);
end;

The TreeView already has left mouse button click (select node) and double-click functionality (expands or contracts a node) but it does not have right-click functionality, so we can call the EditText procedure:

procedure TForm1.TreeView1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
 if button = mbRight then
  if treeview1.Selected <> nil then
   if button=mbRight then
    treeview1.Selected.EditText;
end;

Back to Tutorial

Back to Palette List