Display a file search result of a folder into a Virtual Treeview

LMuadDIb
LMuadDIb used Ask the Experts™
on
I am using TFindFile (http://www.delphiarea.com/products/delphi-components/findfile/) to search a folder for certain files, and I want to display the results into a Virtual Treeview (http://www.delphi-gems.com/index.php?option=com_content&task=view&id=12&Itemid=38)

well, I can not figure out how to implement the VT component LOL
I would want to do it real time so when the TFIleFindFoundMatch event is fired I would like a VT Node added:EXAMPLE>

procedure TdmExtract.FindFile1FileMatch(Sender: TObject;
  const FileInfo: TFileDetails);
begin
  CurrentFileInfo:= FileInfo;
  TThread.Synchronize(nil, UpdateQueue);
end;

procedure TdmExtract.UpdateQueue;    //thread safe, TThread.Synchronize
var
  FileInfo: PFileDetails;
  Node: PVirtualNode;
  Data: ^TFileDetails;
  i,Idx: integer;
begin

  Log(mtWarning, CurrentFileInfo.Name);

  FormLog.VST.BeginUpdate;

  Idx := CurrentFileInfo.Index;
 
    Node := FormLog.VST.AddChild(nil);
    Data := FormLog.VST.GetNodeData(Node);

    //Create link to your data record into VST node
    Data := @CurrentFileInfo;

    FormLog.VST.EndUpdate;

end;


this works for root nodes with no caption
sorry, but I am lost with this component

can some one supply with code to show me how to add the nodes to the VST?
Would like to keep the structure intact (child nodes because its recursive)
Delphi 2010
Thank you for your time

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Geert GOracle dba
Top Expert 2009

Commented:
What is @CurrentFileInfo ?
Where did you define this ?

I think you will need to create a new (guessing descendant of) TObject
with the CurrentFileInfo and assign that to the data

Data.FileInfo := TCurrentFileInfo.Create;
Data.FileInfo.FileName := CurrentFileInfo.FileName;

you'll need to provide a bit more code for help

Author

Commented:
maybe we should start from scratch
because I fooled around with a few things (and what you mentioned) and I am still confused
when this event is fired I would like the passed const "FileInfo:TFileDetails" into the VST data node

procedure TdmExtract.FindFile1FileMatch(Sender: TObject;
  const FileInfo: TFileDetails);
var
  Node: PVirtualNode;
  Data: ^TFileDetails;
begin
  Node :=VST.AddChild(nil);
  Data := VST.GetNodeData(Node);
/// ....
end;

how do I assign the "const FileInfo: TFileDetails" from the event to the VST data?
I was creating a record and copying the information from the "const FileInfo: TFileDetails" to the record and then assigned the record to .Data

it would compile, but I couldnt access the information in the data.
I would use a different component, but the list might hold hundreds of files if not thousands.
thanx for the help
Oracle dba
Top Expert 2009
Commented:
do you have any experience with the virtual treeview ?
It is one of the more difficult components to get used to!

i'll copy and adapt the necessary parts for the virtual treeview to work from my own code
with some comments

the code is from a old label designer of mine

the only procedures which you don't need to implement from the snippet are tvLabelPaintText and tvLabelGetHint

you would need to work on the function AddNode

i hope this gives you a idea of how to use the virtual treeview
type
  // This is tobject descendant to hold a filename and filesize
  TTVData = class(TObject)
  private
    FFileName: string;
    FFileSize: Integer;
  public
    constructor Create;
    destructor Destroy; override;
    property FileName: string read FFileName write fFileName;
    property FileSize: integer read FFileSize write fFileSize;
  end;
 
  // this is a record structure for the virtual treeview to hold the object
  PTV = ^RTV;
  RTV = record
    Data: TTVData;
  end;
 
  TfrmLabelDesigner = (TForm)
    tvLabel: TVirtualEditTree;
    procedure tvLabelGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Column: TColumnIndex; TextType: TVSTTextType;
      var CellText: WideString);
    procedure tvLabelFreeNode(Sender: TBaseVirtualTree;
      Node: PVirtualNode);
    procedure tvLabelPaintText(Sender: TBaseVirtualTree;
      const TargetCanvas: TCanvas; Node: PVirtualNode;
      Column: TColumnIndex; TextType: TVSTTextType);
    procedure tvLabelGetHint(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Column: TColumnIndex; var LineBreakStyle: TVTTooltipLineBreakStyle;
      var HintText: WideString);
  public
    constructor Create(AOwner: TComponent); override;
    function AddNode(ParentNode: PVirtualNode; aFileName: string; aFileSize: integer): PVirtualNode;
  end;
 
constructor TfrmLabelDesigner.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  tvLabel.NodeDataSize := SizeOf(RTV);
end;
 
procedure TfrmLabelDesigner.tvLabelGetText(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
  var CellText: WideString);
var Nd: PTV;
begin
  if Node <> Nil then
  begin
    ND := Sender.GetNodeData(Node);
    if ND.Data <> Nil then
    begin
      case Column of
        0: CellText := ND.Data.FileName;
        1: CellText := IntToStr(ND.Data.FileSize);
      end;
    end;
  end;
end;
 
procedure TfrmLabelDesigner.tvLabelFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
var ND: PTV;
begin
  if Node <> Nil then
  begin
    Nd := Sender.GetNodeData(Node);
    if (Nd <> Nil) and (Nd.Data <> Nil) then FreeAndNil(Nd.Data);
  end;
end;
 
procedure TfrmLabelDesigner.tvLabelPaintText(Sender: TBaseVirtualTree;
  const TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
  TextType: TVSTTextType);
var PNode, FNode: PVirtualNode;
  D: PTV;
begin
  if Column = 0 then
  begin
    PNode := Node;
    while (PNode <> Nil) and (Sender.GetNodeLevel(PNode) > 1) do PNode := PNode.Parent;
    FNode := Sender.FocusedNode;
    while (FNode <> Nil) and (Sender.GetNodeLevel(FNode) > 1) do FNode := FNode.Parent;
    if FNode = PNode then
    begin
      if Sender.GetNodeLevel(Node) >= 1 then
      begin
        if Node.ChildCount > 0 then
        begin
          if Sender.Selected[Node] then
            TargetCanvas.Font.Color := clWhite
          else
            TargetCanvas.Font.Color := clNavy;
        end else
        begin
          if Sender.Selected[Node] then
          begin
            if Sender.IsEditing then
              TargetCanvas.Font.Color := clGreen
            else
              TargetCanvas.Font.Color := clLime;
          end else TargetCanvas.Font.Color := clGreen;
        end;
      end;
    end else TargetCanvas.Font.Color := clBlack;
  end;
end;
 
procedure TfrmLabelDesigner.tvLabelGetHint(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Column: TColumnIndex;
  var LineBreakStyle: TVTTooltipLineBreakStyle; var HintText: WideString);
var D: PTV;
begin
  if Node <> Nil then
  begin
    D := Sender.GetNodeData(Node);
    if (D <> Nil) and (D.Data <> Nil) then
    begin
      case Column of
        0: HintText := D.Data.FileName;
        1: HintText := IntToStr(D.Data.FileSize);
      end;
    end;
  end;
end;
 
function TfrmLabelDesigner.AddNode(ParentNode: PVirtualNode; aFileName: string; aFileSize: integer): PVirtualNode;
var SD: PTV;
begin
  Result := tvLabel.AddChild(ParentNode);
  SD := tvLabel.GetNodeData(Result);
  SD.Data := TTVData.Create;
  SD.Data.FileName := aFileName;
  SD.Data.FileSize := aFileSize;
end;

Open in new window

Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Author

Commented:
thanks
I have tried in the past to use the VT, otherwise I have never been able to get it work except for really simple projects. And I have been working on this most of the day with no luck LOL GOing over all kinds of demos.

I will try your code later tonight. Im frazzled atm +)
thanx again

Author

Commented:
perfect! worked great and easy to understand
I just wished I could have found similiar code before spending so much time on this

Geert GOracle dba
Top Expert 2009

Commented:
lol, i wasted a lot of time on this thing too

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial