Delphi, VirtualStringTree - classes (objects) instead of records

前端 未结 4 988
-上瘾入骨i
-上瘾入骨i 2020-12-30 16:39

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

Should I declare it standard (but in this case - tricky) way like that:

PNode = ^         


        
相关标签:
4条回答
  • 2020-12-30 17:09

    Just add an object reference to your record. Use the OnInitNode and OnFreeNode events to create and destroy your object.

    0 讨论(0)
  • 2020-12-30 17:11
    1. Setup datasize for holding object

      vstTree.NodeDataSize := SizeOf(TMyObject); 
      
    2. Get the datasize holder and bind to your object

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

      or

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

      or
      use vstTree.InsertNode method

    3. To free the binding object hookup the OnFreeNode event

      vstTree.OnFreeNode := FreeNodeMethod;
      

      with

      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;
      
    0 讨论(0)
  • 2020-12-30 17:11

    And you can free your object in OnFreeNode event.

    0 讨论(0)
  • 2020-12-30 17:13

    you could create the object instance after receiving the node data, as in :

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

    or you could try and assign it directly

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

    0 讨论(0)
提交回复
热议问题