Editing TreeNode.Text


Control Caption Name
TTreeView   tv_eg1
TButton Add but_Add
TButton Remove but_Remove
TMemo   mem_History




   procedure TForm1.tv_eg1Editing(Sender: TObject; Node: TTreeNode;  var AllowEdit: Boolean);
   begin
         {All but root may be edited}
      AllowEdit := (Node <> tv_eg1.Items[ 0 ]);
   end;
The OnEditing event is used to allow or disallow editing of a nodes text. In this example the only restriction is that the root node may not be renamed. (This event is called before the node is edited)



   procedure TForm1.tv_eg1Edited(Sender: TObject; Node: TTreeNode;  var S: String);
   begin
      mem_History.Lines.Insert(  0,  Node.Text + ' --> renamed --> ' + S  );
   end;
The OnEdited is called after an edit has taken place. Node.Text still has the Node's original name. The variable S contains the new name. You can change S back to the original name, or to any other value as you see fit.

Here all renames are logged to the TMemo.




   procedure TForm1.tv_eg1KeyUp(Sender: TObject; var Key: Word;  Shift: TShiftState);
   begin
      case Key of
         VK_F2 :
         begin
            if(  tv_eg1.Selected <> nil  ) then
               tv_eg1.Selected.EditText;
         end;

         VK_DELETE :
         begin
            if(  tv_eg1.Selected <> nil  ) then
               if(  not tv_eg1.IsEditing  ) then
                  but_Remove.Click;
         end;
      end;
   end;
The OnKeyUp event is used here to allow the user to use the F2 and Delete keys to make working with the TreeView easier.

F2 If there is a selected node then go into edit mode (tv_eg1.Selected.EditText)
Delete
  1. If there is a selected node
  2. If not in edit mode
  3. Delete the node

The "if not in edit mode" test may seem unnecessary, but its not. When the user is in edit mode and pressed Delete, then the character after the insertion pointer must be deleted. If this check where not there, then pressing Delete while in edit mode would delete the selected node.






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

ilto:dart@pobox.com">Andre .v.d. Merwe And may not be copied or mirrored without my permission.