Recent

Author Topic: Virtual TreeView - delete node using node index  (Read 4177 times)

JD

  • Hero Member
  • *****
  • Posts: 1848
Virtual TreeView - delete node using node index
« on: February 02, 2018, 07:36:53 pm »
Hi there everyone,

I want to delete a node in a VirtualTreeView using the node's index. I know how to delete the node using the FocusedNode like this

Code: Pascal  [Select][+][-]
  1. VST.DeleteNode[VST.FocusedNode]
  2.  

However, in my use case, the node should only be deleted after the corresponding record has been deleted in a remote database. As this can the delayed due to network problems, a user might click elsewhere in the VirtualTreeView or the form and there is no guarantee that the actual focused node is the same node the user wanted to delete earlier. So I want to save the node's index and then use that index to remove the node from the VirtualTreeView. Something like this

Code: Pascal  [Select][+][-]
  1. VST.DeleteNode[idxNode]
  2.  

How do I get the numerical index of the focused node?

Thanks,

JD
 
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

balazsszekely

  • Guest
Re: Virtual TreeView - delete node using node index
« Reply #1 on: February 02, 2018, 08:18:52 pm »
Hi JD,

You should assign to each node a UniqueID, like this:
Code: Pascal  [Select][+][-]
  1. type
  2.    PData = ^TData;
  3.    TData = record
  4.      FUniqueID: Integer;
  5.      //other properties here
  6.    end;
  7.  
  8. var
  9.   UniqueID: Integer = 0;
  10.  
  11. //add node to tree
  12. var
  13.   Node: PVirtualNode;
  14.   Data: PData;
  15. begin
  16.   Node := VST.AddChild(nil);
  17.   Data := VST.GetNodeData(Node);
  18.   UniqueID := UniqueID + 1;
  19.   Data^.FUniqueID := UniqueID;
  20. end;        


When you wish to delete a specific node(focused or not), just loop through the tree nodes, find the node with the specific ID, make sure you immediately exit the loop right after you delete the node.
 


JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Virtual TreeView - delete node using node index
« Reply #2 on: February 03, 2018, 12:29:49 pm »
Thanks a lot for the tip GetMem. The record represented by the node has a unique ID so I just used that to remove the node.

Cheers,

JD
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

 

TinyPortal © 2005-2018