Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6766075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:47:13+00:00 2026-05-26T14:47:13+00:00

I am using Delphi2010 and trying to wrap my head around using VirtualStringTree. I’ve

  • 0

I am using Delphi2010 and trying to wrap my head around using VirtualStringTree.

I’ve been trying to get it to work with objects and was having no luck until I followed Philipp Frenzel’s Virtual TreeView tutorial which I found on the soft-gems.net web site. What I’ve come up with so far works but I don’t think I’m handling the subnodes (i.e. childnodes) correctly.

The only thing I’ve been able to get working is to link the whole object in again for each child and then just display the field I need – but it just feels so wrong.

Suggestions/feedback much appreciated.


I have list of objects that I’m trying to hookup with VirtualStringTree where I’m trying to achieve something like this where one of the fields will act as the label to the parent and the rest of the fields show up as childnodes.

  • Robert Lane
    • Male
    • 35
    • Los Angeles
    • Brunette
  • Jane Doe
    • Female
    • 19
    • Denver
    • Redhead

This how my class is set up.

type
  PTreeData = ^TTreeData;
  TTreeData = record
    FObject : TObject;
  end;

  TCustomerNode = class(TObject)
  private
    fName: string;
    fSex: string;
    fAge: integer;
    fHair: string;
    //...
  public
    property Name: string read fName write fName;
    //...
  end;

Once I populate the objects I add them to another class (CustomerObjectList) based off of TList which is mentioned below.

Here is where I connect VirtualStringTree with my object list

procedure TfrmMain.btnLoadDataClick(Sender: TObject);
var
  i, j : integer;
  CustomerDataObject: TCustomerNode;
  RootXNode, XNode: PVirtualNode;
  Data: PTreeData;
begin
  vstree.NodeDataSize := SizeOf( TTreeData );

  vstree.BeginUpdate;
  for i := 0 to CustomerObjectList.Count - 1 do
  begin
    CustomerDataObject := CustomerObjectList[i];

    //load data for parent node
    RootXNode := vstree.AddChild(nil);
    Data  := vstree.GetNodeData(RootXNode);
    Data^.FObject:= PINodeSource;

    //now add children for rest of fields
    //Isn't there a better way to do this?
    for j := 1 to NUMBERofFIELDS -1 do  //first field is label for parent so -1
    begin
      XNode := vstree.AddChild(RootXNode);
      Data  := vstree.GetNodeData(XNode);
      Data^.FObject:= PINodeSource;
    end;

  end;
  vstree.EndUpdate; 
end;    

procedure TfrmMain.vstreeGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
 Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
var
  Data : PTreeData;
begin
  Data := vstObjects.GetNodeData(Node);
  ////if Node.ChildCount  = 0 then //edit - oops typo!
  if Node.ChildCount  > 0 then
  begin
    CellText := TCustomerNode(Data.FObject).Name;
    exit;
  end;

  //handle childnodes
  case Node.Index of
    0:  CellText := TCustomerNode(Data.FObject).Sex;
    1:  CellText := IntToStr(TCustomerNode(Data.FObject).Age);
    2:  CellText := TCustomerNode(Data.FObject).Hair;
    3:  CellText := TCustomerNode(Data.FObject).City;
  end;  
end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:47:13+00:00Added an answer on May 26, 2026 at 2:47 pm

    You don’t need to load all the data into the tree. You can use the ‘virtualness’ of the tree. Here’s how I would do it.

    Set the RootNodeCount of the tree to the number of records in CustomerObjectList:

    vstree.RootNodeCount := CustomerObjectList.Count;
    

    Then, in the OnInitChildren event, set the child count of level 0 nodes to the number of properties you want to display:

    procedure TfrmMain.vstreeInitNode(Sender: TBaseVirtualTree; ParentNode, Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
    begin
      if Sender.GetNodeLevel(Node) = 0 then
      begin
        Sender.ChildCount[Node] := 4;
    
        // Comment this out if you don't want the nodes to be initially expanded
        Sender.Expanded[Node] := TRUE;
      end;
    end;
    

    Now, just retrieve the correct data in the OnGetText event.

    procedure TfrmMain.vstreeGetText(Sender: TBaseVirtualTree;
      Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
      var CellText: string);
    begin
      if Column <= 0 then
      begin
        if Sender.GetNodeLevel(Node) = 0 then
          CellText := CustomerObjectList[Node.Index].Name else
        if Sender.GetNodeLevel(Node) = 1 then
        begin
          case Node.Index of
            0: CellText := CustomerObjectList[Parent.Node.Index].Sex;
            1: CellText := CustomerObjectList[Parent.Node.Index].Age;
            2: CellText := CustomerObjectList[Parent.Node.Index].Hair;
            3: CellText := CustomerObjectList[Parent.Node.Index].City;
          end; // of case
         end;
    end;
    

    You’ll probably want to add in some more range checking, just in case.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to find a good-looking design using Aero in Delphi 2010.
I am trying to make a modular application in delphi2010 using BPL. Problem is
Is there a way (on windows using Delphi 2010) to get the number of
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Trying to insert values with Unicode Chars into a MySQL-database using Delphi 2010 and
I am trying to save (compress) a .zip file using JclCompression with the JCL
I am trying to remotely read a binary (REG_BINARY) registry value, but I get
I am trying to compile a Delpho 2010 project using the msbuild tool. Unfortunately
I'm using Delphi 2010, and I am trying to allow the user to select
I am having problem with IdTCPclient.connected function from Indy in Delphi. I am using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.