AdvDirectoryTreeView: Find dir and show

Dear all,

I am using advDirTreeView to mage files.
What I would like to do, is to open the tree of directories at the same position, the user has opened some file before.
I tried multiple things, e.g iterating over the nodes, which does not work because the nodes are only loaded loaded once they are expanded. To open the tree in full extend and hence have all nodes is also not a good ides in a complex network environment.

What would be the way to find, select and expand the node that corresponds to a folder XXX ?

Best
Michael

Sorry, the explanation is not clear. The behaviour you describe is already available in AdvDirectoryTreeView. It first loads all directories (for example with AdvDirectoryTreeView.LoadDrives or AdvDirectoryTreeView.LoadDirectory) and then when clicking the expand node, the sub folders are dynamically loaded. Please explain more in detail what exactly it is that you want to achieve?

Hi,

say I know that the recent project dir is D:\myprojects\Aproject or the last time a file was loaded from that folder.
So, when I open a form using the advDirTreeView, I would like to programmly pre-select this folder (and expand all the nodes leading to this folder).
How can I achieve this without loading ALL directories with all files on the system and then search the whole tree for it?

Best
Michael

You can use GetCurrentDir for knowing the last dir the user has opened:


  AdvDirectoryTreeView1.LoadDirectory(GetCurrentDir);
Well yes, but this is not the point.
I want to have the node selected by program in the TreeView (and extended...)

Michael

You will have to write your own loop then, and cast the node to TAdvDirectoryTreeViewNode. After casting you can use the FileName property to see if the directory matches the one the user has last used. Other than that approach, there is no magic solution unfortunately.

OK,that was my initial idea but that would require to load ALL dirs and drives before to have the nodes.
Ccan I at least trigger the loading of sub-nodes for a single drive like the expand=True Property does when using LoadDirs for all drives on the computer?
Once I have this nodes filles I can of course loop through them, but in default I do not have then to loop through...



Michael 

Yes, you can with this code:




type
  TAdvDirectoryTreeViewOpen = class(TAdvDirectoryTreeView);


procedure TForm1.FormCreate(Sender: TObject);
begin
  AdvDirectoryTreeView1.LoadDrives(False);
  TAdvDirectoryTreeViewOpen(AdvDirectoryTreeView1).AddSubNodes(AdvDirectoryTreeView1.Nodes[0]);
  AdvDirectoryTreeView1.Nodes[0].Expand;
end;

Thanks a lot,I guess that will implement some "InitialDir" behaviour

Best
Michael
Just in case that someone has the same issue.

That would be the code to implement some "InitialDir" behaviour... not much optimized but works...

Michael


procedure TXXOpenDlg.RestoreFolder(aFolder: String);
var
  i: Integer;
  nd: TAdvTreeViewNode;
  mynode, mychild: TAdvDirectoryTreeViewNode;
  s: String;
  basedrive: String;
  found: Boolean;
begin
  if aFolder = '' then
    Exit;
  basedrive := ExtractFileDrive(aFolder);

  found := False;
  // Loop over nodes to find the correct one if there
  for i := 0 to tvFileList.Nodes.count - 1 do
  begin
    mynode := TAdvDirectoryTreeViewNode(tvFileList.Nodes);
    s := mynode.filename;

    if Uppercase(ExtractFileDrive(s)) = Uppercase(basedrive) then
    begin
      // Load & Expand node
      TAdvDirectoryTreeViewOpen(tvFileList).AddSubNodes(mynode);
      mynode.Expand;
      mychild := TAdvDirectoryTreeViewNode(mynode.getnext);
      repeat
        if mychild <> NIL then
        begin
          s := mychild.filename;

          // found final path node?
          if Uppercase(ExtractFilePath(s)) = Uppercase(aFolder) then
          begin
            TAdvDirectoryTreeViewOpen(tvFileList).AddSubNodes(mychild);
            tvFileList.SelectNode(mychild);
            mychild.Expand;
            Exit;
          end;
          // is correct path
          if Copy(aFolder, 1, length(s)) = s then
          begin
            TAdvDirectoryTreeViewOpen(tvFileList).AddSubNodes(mychild);
            mychild.Expand;
            mychild := TAdvDirectoryTreeViewNode(tvFileList.GetFirstChildNode(mychild));
          end
          Else
          mychild := TAdvDirectoryTreeViewNode(mychild.getnext);
        end;
      until (mychild = NIL);
    end;
  end;
end;