Code to populate the dirTree (REVISED)
procedure TForm1.FormClick(Sender: TObject);
var
sr: TSearchRec;
FileAttrs: Integer;
theRootNode : tTreeNode;
theNode : tTreeNode;
begin
FileAttrs := faDirectory; // Only care about directories
theRootNode := DirTree.Items.AddFirst(nil,'c:\');
if FindFirst('c:\*.*', FileAttrs, sr) = 0 then
begin
repeat
if (sr.Attr and FileAttrs) = sr.Attr then
begin
theNode := dirTree.Items.AddChild(theRootNode,sr.name);
AddDirectories(theNode,'c:\'+sr.Name);
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
// DirTree.FullExpand;
end;
*Code to populate FileTree (REVISED) *
procedure TForm1.FilteredTV(theDir: string;ext:String;startNode:tTreeNode);
var
sr: TSearchRec;
FileAttrs: Integer;
theNode : tTreeNode;
begin
if copy(ext,1,1)<>'.' then ext := '.'+ext;
FileAttrs := faAnyfile;
if startNode = nil then
StartNode := FileTree.Items.AddFirst(nil,theDir);
if FindFirst(theDir+'\*.*', FileAttrs, sr) = 0 then
begin
repeat
if (sr.Attr=faDirectory) and (copy(sr.Name,1,1)<>'.') then
begin
theNode := FileTree.Items.AddChild(StartNode,sr.name);
theNode.ImageIndex := 0; // Use folder image for directories
FilteredTV(theDir+'\'+sr.name,ext,theNode);
end
else
if ((sr.Attr and FileAttrs) = sr.Attr) and (ExtractFileExt(sr.name)=ext)
then
begin
theNode := FileTree.Items.AddChild(StartNode,sr.name);
theNode.ImageIndex := -1; // No image for files
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
FileTree.FullExpand;
end;
Additional procedure to add to form
procedure TForm1.AddDirectories(theNode: tTreeNode; cPath: string);
var
sr: TSearchRec;
FileAttrs: Integer;
theNewNode : tTreeNode;
begin
FileAttrs := faDirectory; // Only care about directories
if FindFirst(cPath+'\*.*', FileAttrs, sr) = 0 then
begin
repeat
if ((sr.Attr and FileAttrs) = sr.Attr) and (copy(sr.Name,1,1) <> '.')
then
begin
theNewNode := dirTree.Items.AddChild(theNode,sr.name);
AddDirectories(theNewNode,cPath+'\'+sr.Name);
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
You need to add an image list to your form, add a folder icon to it (there is one in the borland common files) and then associated the image list with the directory treeview and the filetree treeview
EXAMPLE OF HOW TO CALLED FILTEREDTV procedure
Attach the following code to the OnClick event of the directory tree
procedure TForm1.DirTreeClick(Sender: TObject);
var
cBuild : string;
theNode : tTreeNode;
begin
if DirTree.Selected <> nil then
begin
theNode := DirTree.Selected;
cBuild := theNode.Text;
while theNode.Parent <> nil do
begin
cBuild := theNode.Parent.Text+'\'+cBuild;
theNode := theNode.Parent;
end;
cBuild := stringReplace(cBuild,'\\','\',[rfReplaceAll]);
FilteredTV(cBuild,'pdf',nil);
end;
end;