TAdvDirectoryTreeView - how to expand to a known path?

Hi,

If I have a path in a string “C:\Folder1\Folder2” – how do I expand just those folders in TAdvDirectoryTreeView, e.g. a SetPath method?

Thanks
D10.3

Did you try:
advdirectorytreeview.LoadDirectory('c:\Folder1\Folder2', true);

Yes, tried that, but it opens just that folder tree.

I need a full explorer window with that path expanded. The requirement is “go to the last path the user had open on restart with ability to switch drives/folders”

Such function is at this moment not available.

This class helper provides it for you:

type
  TAdvDirectoryTreeViewHelper = class helper for TAdvDirectoryTreeView
    function FindNodeByPath(AStartNode: TAdvTreeViewNode; const APath: string):    TAdvTreeViewNode;
  private
    function FindNodeRecursive(ANodes: TAdvTreeViewNodes; AParts: TStringList;  APartIndex: Integer): TAdvTreeViewNode;
  end;

function TAdvDirectoryTreeViewHelper.FindNodeRecursive(ANodes: TAdvTreeViewNodes; AParts: TStringList; APartIndex: Integer): TAdvTreeViewNode;
var
  i: Integer;
  Node: TAdvTreeViewNode;
  CanExpand: boolean;
begin
  Result := nil;

  if APartIndex >= AParts.Count then
    Exit;

  CanExpand := true;

  for i := 0 to ANodes.Count - 1 do
  begin
    Node := ANodes[i];
    if SameText(Trim(Node.Text[0]), AParts[APartIndex]) then
    begin
      // If this is the last part, we found our target
      if APartIndex = AParts.Count - 1 then
      begin
        Result := Node;
        Exit;
      end;
 
      // Otherwise recurse into this node's children for the next part
      if not Node.Expanded then
      begin
        DoBeforeExpandNode(Node.VirtualNode, CanExpand);
        ExpandNode(Node);
      end;

      Result := FindNodeRecursive(Node.Nodes, AParts, APartIndex + 1);
      if Assigned(Result) then
        Exit;
    end;
  end;
end;

function TAdvDirectoryTreeViewHelper.FindNodeByPath(AStartNode: TAdvTreeViewNode; const APath: string): TAdvTreeViewNode;
var
  Parts: TStringList;
  StartNodes: TAdvTreeViewNodes;
  i: integer;
begin
  Result := nil;

  Parts := TStringList.Create;
  try
    ExtractStrings(['\'], [], PChar(APath), Parts);

    for i := Parts.Count - 1 downto 0 do
    begin
      Parts[i] := Trim(Parts[i]);
      if (Parts[i] = '') or (Parts[i].EndsWith(':')) then
        Parts.Delete(i);
    end;

    if Parts.Count = 0 then
      Exit;

    if Assigned(AStartNode) then
      StartNodes := AStartNode.Nodes
    else
      StartNodes := Self.Nodes;

    Result := FindNodeRecursive(StartNodes, Parts, 0);
  finally
    Parts.Free;
  end;
end;

and this class helper can be used in the following way:

var
  n: TAdvTreeViewNode;
begin
  n :=  AdvDirectoryTreeView1.FindNodeByPath(AdvDirectoryTreeView1.Nodes[0], '\Folder1\Folder2');

  if Assigned(n) then
  begin
    advdirectorytreeview1.ScrollToNode(n);
    advdirectorytreeview1.SelectNode(n);
  end;
end;

We will further check whether to integration this function by default in TAdvDirectoryTreeView in a future update.

Thank you Bruno !

(Just a note, the usage needs AdvDirectoryTreeView1.LoadDrives(TRUE) first :slight_smile: