Note that there are some explanatory texts on larger screens.

plurals
  1. POVirtualStringTree - Correct way to add/handle subnodes/childnodes when using objects?
    primarykey
    data
    text
    <p>I am using Delphi2010 and trying to wrap my head around using VirtualStringTree.</p> <p>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.</p> <p>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.</p> <p>Suggestions/feedback much appreciated.</p> <hr> <p>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. </p> <ul> <li>Robert Lane <ul> <li>Male </li> <li>35</li> <li>Los Angeles</li> <li>Brunette</li> </ul></li> <li>Jane Doe <ul> <li>Female</li> <li>19</li> <li>Denver</li> <li>Redhead</li> </ul></li> </ul> <p>This how my class is set up. </p> <pre><code>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; </code></pre> <p>Once I populate the objects I add them to another class (CustomerObjectList) based off of TList which is mentioned below. </p> <p>Here is where I connect VirtualStringTree with my object list</p> <pre><code>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 &gt; 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; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload