Add and remove nodes

This is quick example of adding and removing nodes to/from a TTreeView.

Start a new project, drop a TTreeView and two TButtons onto the form


Control Caption Name
TTreeView   tv_eg1
TButton Add but_Add
TButton Remove but_Remove


Adding a node

When adding a TTreeNode you need to know where you want to add it to. In other words you must know which node, if any, is to be the new node's parent

A root node has no parent ie TTreeNode.Parent= nil
Other nodes are 'children' of a parent node. These node's parent may be a root node or another child node.

Most often you'd want to add a node as a child to the currently selected item. Before you do this however you must ensure that there is a selected node. To do this test if TTreeView.Selected = nil


   if( tv_eg1.Selected = nil ) then
   begin
   end;

There are two reasons why TTreeView.Selected may be nil

  1. The TTreeView is empty, there are node to be selected
  2. There are nodes, but the user has not selected one yet

Check if the tree is empty


   if(  tv_eg1.Items.Count = 0  ) then
   begin
   end;


We now have the methods nessasary to determine the state of the TTreeView so lets add a node


Add the Root Node


To add an item to a TreeView you call one of the TTreeView.Items.Addxxx
functions. Such as AddFirst,  AddFirstChild,  AddChild etc.


   with tv_eg1.Items.AddFirst(  nil,  'Root'  ) do
   begin
      Selected := true;
   end;

Since we'r adding a root node use AddFirst. This adds a new node as a sibling of the node passed in parameter 1. However we'r adding the root, so there are no sibling nodes. So pass nil as parameter 1.


Adding a child node

      {Get a name for the new node}
   InputQuery(  'New Node',  'Caption ?',  sText  );

      {Add the node as a child of the selected node}
   with tv_eg1.Items.AddChild(  tv_eg1.Selected,  sText  ) do
   begin
      MakeVisible;
   end;

Here the user is asked for a name for the node. Then the node is added as a child to the currently selected node.
When you add a node to a TTreeView that node migh not be shown so the MakeVisible command shows the node that has just been added.



Now the full source code for but_Add's OnClick event



procedure TForm1.but_AddClick(Sender: TObject);
var
   sText : string;
begin
      {If nothing is selected}
   if(  tv_eg1.Selected = nil  ) then
   begin
         {If there is no root node}
      if(  tv_eg1.Items.Count = 0  ) then
      begin
            {Add the root node}
         with tv_eg1.Items.AddFirst(  nil,  'Root'  ) do
		 begin
               {Select the root node} 
            Selected := true;
         end;
      end
      else begin
            {There is a root, so user must first select
            a node before adding a new node}
         MessageBeep(  -1  );
         ShowMessage(  'Select a parent node'  );
         Exit;
      end;
   end
   else begin
         {Get a name for the new node}
      InputQuery(  'New Node',  'Caption ?',  sText  );

         {Add the node as a child of the selected node}
      with tv_eg1.Items.AddChildFirst(  tv_eg1.Selected,  sText  ) do
      begin
         MakeVisible;
      end;
   end;
end;


Removing a node


procedure TForm1.but_RemoveClick(Sender: TObject);
begin
      {Make sure somthing is selected, before trying to
        delete it}
   if(  tv_eg1.Selected = nil  ) then
   begin
      MessageBeep(  -1  );
      ShowMessage(  'Nothing selected'  );
      Exit;
   end;

      {Dont allow user to delete the root node}
   if(  tv_eg1.Selected.Level = 0  ) then
   begin
      MessageBeep(  -1  );
      ShowMessage(  'Cant delete the root node'  );
      Exit;
   end;


      {Delete the node}
   tv_eg1.Selected.Delete;
end;

Nothing complex here...

  1. Check that there is a selected node
  2. Don't allow the root to be deleted
  3. Delete the delected node

You can remove the if block that does the checking for the root, if you wish.






All information on these www pages is copyright (©) 1997 Andre .v.d. Merwe And may not be copied or mirrored without my permission.

All information on these www pages is copyright (©) 1997 Andre .v.d. Merwe And may not be copied or mirrored without my permission.