Recent

Author Topic: Expanding nodes in a TShellTreeView  (Read 1170 times)

petevick

  • Sr. Member
  • ****
  • Posts: 337
Expanding nodes in a TShellTreeView
« on: February 03, 2022, 12:52:19 pm »
I have the following code, which I came across on here and can't find it now!!, in a project that expands a named node in a TShellTreeView, but it only seems to work on the first level nodes...

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.NodeByText(ToDo:String; aNodeText:String);
  3. var
  4.   vNode : TTreeNode;
  5.   vLastNode : TTreeNode;
  6. begin
  7.   for vNode in TreeView.Items do begin
  8.     if vNode.Text = aNodeText then begin
  9.       vLastNode := vNode;
  10.       vNode.Selected := True;
  11.     end;
  12.   end;
  13.     vLastNode.Expanded:=True;
  14. end;
  15.  
...I've tried modifying it to include all node levels but I keep on getting errors. I would be grateful for any help before I loose more of what little hair I have.  ::)
Pete Vickerstaff
Linux Mint 21.1 Cinnamon, Lazarus 3.2, FPC 3.2.2

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
Re: Expanding nodes in a TShellTreeView
« Reply #1 on: February 03, 2022, 01:13:46 pm »
Code: Pascal  [Select][+][-]
  1.     vLastNode.Expanded:=True;
Try (untested)
Code: Pascal  [Select][+][-]
  1.     vLastNode.Expand({Recurse=}True);

petevick

  • Sr. Member
  • ****
  • Posts: 337
Re: Expanding nodes in a TShellTreeView
« Reply #2 on: February 03, 2022, 01:21:23 pm »
Try (untested)
Code: Pascal  [Select][+][-]
  1.     vLastNode.Expand({Recurse=}True);
I'm pretty sure that won't work as the code ends at last first level node before that line
Pete Vickerstaff
Linux Mint 21.1 Cinnamon, Lazarus 3.2, FPC 3.2.2

balazsszekely

  • Guest
Re: Expanding nodes in a TShellTreeView
« Reply #3 on: February 03, 2022, 02:57:35 pm »
@petevick

When you loop through the nodes and expand a particular node inside loop, you will reach the end(items.count) sooner.  Use Node.First, Node.Next instead:
Code: Pascal  [Select][+][-]
  1. procedure ExpandNode(SW: TShellTreeView; ANodeName: String);
  2. var
  3.   Node : TTreeNode;
  4. begin
  5.   Node := SW.Items.GetFirstNode;
  6.   while Node <> nil do
  7.   begin
  8.     if Node.Text = ANodeName then
  9.       Node.Expand(True);
  10.     Node := Node.GetNext;
  11.   end;
  12. end;

petevick

  • Sr. Member
  • ****
  • Posts: 337
Re: Expanding nodes in a TShellTreeView
« Reply #4 on: February 03, 2022, 03:04:13 pm »
@petevick

When you loop through the nodes and expand a particular node inside loop, you will reach the end(items.count) sooner.  Use Node.First, Node.Next instead:
Well that looks easy enough, even I can make out the flow  :o
Thanks yet again GetMem  ;D
Pete Vickerstaff
Linux Mint 21.1 Cinnamon, Lazarus 3.2, FPC 3.2.2

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
Re: Expanding nodes in a TShellTreeView
« Reply #5 on: February 03, 2022, 03:44:23 pm »
Code: Pascal  [Select][+][-]
  1. vLastNode.Expanded;
expands the vLastNode direct children.
Code: Pascal  [Select][+][-]
  1. vLastNode.Expand({Recurse=}True);
.Expand(True) as proposed in my entry and used by GetMem recursively expand vLastNode and all its children.

I think that GetMem's code will needlessly scan subtrees that may have already have been fully expanded.

 

TinyPortal © 2005-2018