[SOLVED] Get the name of a TShellTreeView node lazarus / 31-LCL ---------------------------------------------------------------------- petevick · 2022-02-04 15:19 TShellTreeView is going to be the death of me  In the attached tree view, the folder in the yellow rectangle represents a folder selected by a user, I get it's name by.... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ShellTreeView1.Selected.Text...how would I go about getting the name of the folder in the red rectangle from the selected folder. ---------------------------------------------------------------------- wp · 2022-02-04 16:06 If you know for sure that the red folder is three levels up from the yellow then you can use the Parent of the Parent of the Parent: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ShowMessage(ShellTreeView1.Selected.Parent.Parent.Parent.Text); But you probably mean the node at the first level below "/home/pete/Projects/Folder". Do this in a while loop in which you iterate the parents of the selected node up until you arrive at "/home/pete/Projects/Folder". Untested (or: I intentionally left one detail which you should fix yourself; programming hierarchical structure is a perfect exercise to keep your brain from rusting). Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function FindFirstParentOf(ANode: TTreeNode; UppermostParentText: String): TTreeNode;var  parentNode: TTreeNode;begin  Result := nil;  if ANode = nil then    exit;  while ANode <> nil)  do  begin    parentNode := ANode.Parent;    if (parentNode.Text = UpperMostParentText) then    begin      Result := ANode;      exit;    end;    ANode := parentNode;  end;end; ---------------------------------------------------------------------- Bart · 2022-02-04 16:15 Didn't we have a property for this (FullPath or similar)? Bart ---------------------------------------------------------------------- petevick · 2022-02-04 16:18 Quote from: wp on February 04, 2022, 05:06:03 pmIf you know for sure that the red folder is three levels up from the yellow then you can use the Parent of the Parent of the Parent: But you probably mean the node at the first level below "/home/pete/Projects/Folder". Do this in a while loop in which you iterate the parents of the selected node up until you arrive at "/home/pete/Projects/Folder". Untested (or: I intentionally left one detail which you should fix yourself; programming hierarchical structure is a perfect exercise to keep your brain from rusting).   Thanks for this WP. But the problem is the only thing I know is the selected node name in the yellow rectangle. I need some kind of mechanism to work backwards until I reach the Try This folder, or the top of Untitled Folder node. ---------------------------------------------------------------------- petevick · 2022-02-04 16:19 Quote from: Bart on February 04, 2022, 05:15:14 pmDidn't we have a property for this (FullPath or similar)? Bart If there is I've not come across it. ---------------------------------------------------------------------- wp · 2022-02-04 16:28 Quote from: petevick on February 04, 2022, 05:18:43 pmBut the problem is the only thing I know is the selected node name in the yellow rectangle. I need some kind of mechanism to work backwards until I reach the Try This folder, or the top of Untitled Folder node. The code example in the second part of my post does exactly this. It begins with the node given as a parameter (selected node) and works up the tree until it finds the one whose parent has the specified text. Study it. ---------------------------------------------------------------------- balazsszekely · 2022-02-04 16:34 @petevick We already did something similar yesterday, when we expanded a level x node: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function FindParent(ANode: TTreeNode; ALevel: Integer): TTreeNode;begin  Result := ANode.Parent;  while Result <> nil do  begin    if Result.Level = ALevel then      Break;    Result := Result.Parent;  end;end; procedure TForm1.Button1Click(Sender: TObject);var  Node: TTreeNode;begin  if ShellTreeView1.Selected = nil then    Exit;  Node := FindParent(ShellTreeView1.Selected, 1); //in your image, blue node level is 1  if Node <> nil then    ShowMessage(Node.Text);end; ---------------------------------------------------------------------- petevick · 2022-02-04 17:20 @GetMem and WP. The code that both of you posted works as expected (and thank you very much for posting) based on my original post, I did eventually see the workings of it. But I realise now it's not what I'm after, although it could be a part of the final solution !. I badly explained in my opening post what I was trying to achieve. I need to look at this again and see if I can better explain it. ---------------------------------------------------------------------- Bart · 2022-02-04 20:49 Quote from: petevick on February 04, 2022, 05:19:38 pmQuote from: Bart on February 04, 2022, 05:15:14 pmDidn't we have a property for this (FullPath or similar)? Bart If there is I've not come across it. Perhaps: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TCustomShellTreeView.GetPathFromNode(ANode: TTreeNode): string; Bart ---------------------------------------------------------------------- petevick · 2022-02-05 06:38 Right, it came to me whilst trying to get to sleep last night, I've been really over complicating things. Here is the scenario, the user creates a new folder that in code I name 'Untitled Folder' initially, and to simulate Nero file browser in Linux I use..... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ShellTreeView1.Selected.EditText;....the user edits the folder name, but the edited name doesn't take effect until something like a refresh takes place. I was resetting the .Root property to nil and then back to it's original value, I was then trying to re-expand the node with the edited folder, hence the this post was created. But there was no need to effectively collapse the whole tree, all I had to do was collapse the parent of the selected node, so this morning I quickly came up with this code.... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     pNode:=ShellTreeView1.Selected.Parent;    sNode:=ShellTreeView1.Selected;    pNode.Expand(False);    pNode.Expand(True);    sNode.Selected:=True;....and it works and does exactly what I wanted in all situations at any node depth. I can't thank you all enough for the help you've been providing, this has been a very steep and frustrating learning curve for me. I slept easy last night