So I'm working with Delphi 2010 and it's been a while since I began using the VirtualTreeView (precisely VirtualStringTree)..and it seems that I'm doing something in a wrong way..since things aren't working as I'm expecting.
I'm trying to populate my VST with nodes that point to files/subfolders descriptions stored in records of data and generated by scanning a path given by the user..(more details are shown in the following image)
As it shown nodes are shown in weird way..& no matter what I do nodes data aren't initialized properly..nodes captions for column "File" is the only thing that work well.
& here's the code I use:
1- Node data declaration:
type nodeData=record
Text, Size, Path:String;
ImageIndex:Integer;
end;
PNodeData=^nodeData;
var hashmap:TDictionary<String, PVirtualNode>; // hashmap-> to store parent nodes (Folder)
filesList:TDictionary<Integer,nodeData>; // fileList to store records of data
2-Methods
a) scan a path given by the user
procedure AddAllFilesInDir(const Dir:String);
begin
// it scans the path 'Dir' and extract the "name & size" of each file/folder found in this dir
end;
b) generate the filesList tdictionary... & images are stored in "treeImageLIst" which is linked to the treeview.images property
procedure addFileToList(Name, Size:String);
var d:nodeData;
parent:String;
SHFileInfo :TSHFileINfo;
Icon:TIcon;
begin
parent:=ExtractFileDir(Name);
//Get The Icon That Represents The File/Folder
SHGetFileInfo(PChar(Name), 0, SHFileInfo, SizeOf(SHFileInfo),
SHGFI_ICON or SHGFI_SMALLICON );
Icon := TIcon.Create;
Icon.Handle := SHFileInfo.hIcon;
// set The Name, Size, Path
d.Name:=ExtractFileName(Name);
d.Size:=Size;
d.Path:=parent;
// set the ImageIndex
d.ImageIndex:=Form1.treeImageList.AddIcon(Icon);
// add the node to fileList
filesList.Add(filesList.Count, d);
// Destroy the Icon
DestroyIcon(SHFileInfo.hIcon);
Icon.Free;
end;
c) create the tree "theTree"
procedure createTree();
var theNode, Node:PVirtualNode;
d:PNodeData;
parent:String;
nData:nodeData;
i:integer;
begin
for i := 0 to filesList.Count - 1 do
begin
nData:=filesList.Items[i]; parent:=nData.Path;
if(hashmap.ContainsKey(parent))then theNode:=hashmap.Items[parent]
else theNode:=nil;
Node:=Form1.theTree.AddChild(theNode);
// add a checkbox and make it checked
Node.CheckType:=ctCheckBox;
Node.CheckState:=csCheckedNormal;
// get the newly created node data
d:=Form1.theTree.GetNodeData(Node);
// assign a data to the newly created node
d^:=nData;
// add the node to hashmap if it's a new folder node
if((ExtractFileExt(nData.Text)='')and(not hashmap.ContainsKey(nData.Path+'\'+nData.Text)))
then hashmap.Add(nData.Path+'\'+nData.Text, Node);
end;
Form1.theTree.Expanded[Form1.theTree.TopNode]:=True;
end;
d) the treeview events
procedure TForm1.theTreeFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
var d:PNodeData;
begin
d := Sender.GetNodeData(Node);
Finalize(d^);
end;
procedure TForm1.theTreeGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType; var CellText: String);
var d:PNodeData;
begin
d:=Sender.GetNodeData(Node);
case Column of
0:CellText:=d^.Text;
1:CellText:=d^.Size;
end;
end;
procedure TForm1.theTreeGetImageIndex(Sender: TBaseVirtualTree;
Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
var Ghosted: Boolean; var ImageIndex: Integer);
var d:PNodeData;
begin
d:=Sender.GetNodeData(Node);
if(Kind in [ikNormal, ikSelected])then
begin
if(Column=0)then ImageIndex:=d^.ImageIndex;
end;
end;
I'm really frustrated right now..and I don't know why nodes arent created properly..although I tested the records data & they are created well but when I test the onNodeClick event I found that the data record pointed by the Node returns only the first field ..while the other fields are either empty or they generate an Access violation exception.