* * *

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

JD

  • Hero Member
  • *****
  • Posts: 1659
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 (10, 7) - Lazarus 1.8/FPC 3.0.4, NewPascal, Delphi
Linux Mint Cinnamon  - Lazarus 1.8/FPC 3.0.4, NewPascal

Indy 10.6 series; mORMot; Zeos 7.2.1; SQLite, Firebird, PostgreSQL & MariaDB; VirtualTreeView 5.5.3 R1

GetMem

  • Hero Member
  • *****
  • Posts: 2750
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: 1659
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 (10, 7) - Lazarus 1.8/FPC 3.0.4, NewPascal, Delphi
Linux Mint Cinnamon  - Lazarus 1.8/FPC 3.0.4, NewPascal

Indy 10.6 series; mORMot; Zeos 7.2.1; SQLite, Firebird, PostgreSQL & MariaDB; VirtualTreeView 5.5.3 R1

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus