Delphi, VirtualStringTree - classes (objects) instead of records

I need to use a class instead of writing for a VirtualStringTree node.

Should I declare it in the standard (but in this case, complicated) way:

PNode = ^TNode; TNode = record obj: TMyObject; end; //.. var fNd: PNode; begin fNd:= vstTree.getNodeData(vstTree.AddChild(nil)); fNd.obj:= TMyObject.Create; //.. 

Or should I use TMyObject directly? If so - how ?! What about the purpose (construction) of an object and its release?

Thanks in advance m.

+4
class delphi delphi-7 virtualtreeview
source share
4 answers
  • Setting data to store an object

     vstTree.NodeDataSize := SizeOf(TMyObject); 
  • Get a data holder and bind it to an object

     vstTree.getNodeData(passed in interested node)^ := your object 

    or

     vstTree.getNodeData(vstTree.AddChild(nil))^ := TMyObject.Create; 

    or
    use vstTree.InsertNode method

  • To release the binding of the binding object to the OnFreeNode event

     vstTree.OnFreeNode := FreeNodeMethod; 

    from

     procedure TFoo.FreeNodeMethod(Sender: TBaseVirtualTree; Node: PVirtualNode); var P: ^TMyObject; begin P := Sender.getNodeData(Node); if P <> nil then begin P^.Free; P^ := nil; //for your safety or you can omit this line end; end; 
+8
source share

you can instantiate an object after receiving node data, as in:

 fNd:= vstTree.getNodeData(vstTree.AddChild(nil)); fnd.obj := TMbyObject.Create; 

or you can try and assign it directly

Pointer(Obj) := vstTree.getNodeData(...);

+1
source share

And you can free your object in the OnFreeNode event.

+1
source share

Just add the object link to your post. Use the OnInitNode and OnFreeNode to create and destroy your object.

+1
source share

All Articles