Author Topic: VirtualTreeView (VTV) select node after block delete  (Read 776 times)


  • Full Member
  • ***
  • Posts: 186
  • Keep it simple.
VirtualTreeView (VTV) select node after block delete
« on: May 08, 2021, 02:06:35 pm »

testing VTV I noticed that if I have toMultiselect enabed and delete the selected nodes, the tree looses its focused node. I set toAlwaysSelectNode and noticed, that if I do the first tree node is selected, which is even worse than select no node at all, since it scrolls the tree to the top. Usually, if I am working on nodes deep down a tree I want the focus to stay close, not to jump to the other end of the world.

Finally, after a while of studying man pages and options i vain, I wrote this code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.SpeedButtonDelNodeClick(Sender: TObject);
  3. var
  4.   n,enm,fn,ln: PVirtualNode;
  6. begin
  7.   n := nil;
  8.   fn := nil;
  9.   ln := nil;
  10.   // get the first and last selected node
  11.   for enm in StudyStringTree.SelectedNodes do
  12.     begin
  13.     if not assigned(fn) then fn := enm;
  14.     ln := enm;
  15.     end;
  16.   // find the node to focus after the delete
  17.  if assigned(fn) then
  18.    if assigned(fn^.PrevSibling) then
  19.      n := fn^.prevSibling
  20.    else
  21.      if assigned(ln^.NextSibling) then
  22.        n := ln^.NextSibling
  23.      else
  24.        n := fn^.Parent;
  25.  StudyStringTree.DeleteSelectedNodes;
  26.  // restore the focus
  27.  if assigned(n) then
  28.    FocusNode(StudyStringTree,n);
  29. end;

which works like one would expect a tree to behave: if child nodes are deleted, and if there are siblings left, it will focus the previous or the next remaining sibling, if no sibling is left, it will focus the parent node.

Did I miss something, does the Treeview have any meaningful way to keep a node adjacent to the deleted nodes focused, or is my code an adequate solution for the problem?

Thnx, Armin.
« Last Edit: May 08, 2021, 02:12:37 pm by Nimral »
Lazarus 2.0.12 & 2.3.0  (trunk) on Windows 10, Raspberry Pi OS "Buster", macOS Catalina, macOS BigSur, VMWare Workstation 15


TinyPortal © 2005-2018