delphiselectvirtualtreeview

Cannot select the root node in Virtual Tree View


I am using Delphi XE3 with Virtual Tree View.

My codes are below:

type
  TMyData = record
    Caption: string;
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
  RootNode: PVirtualNode;
  PData: ^TMyData;
begin
  RootNode := tvItems.AddChild(nil);
  PData := tvItems.GetNodeData(RootNode);
  PData^.Caption := 'This is a test node';
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  tvItems.NodeDataSize := SizeOf(TMyData);
end;

procedure TForm1.tvItemsGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
  Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
var
  PData: ^TMyData;
begin
  if Assigned(Node) then
  begin
    PData := tvItems.GetNodeData(Node);

    if Assigned(PData) then
      Celltext := PData^.Caption;
  end;
end;

When I click the "Button1", the root node will be created. However, when my mouse clicks the node text, it will not be selected.

Some of my findings:

  1. One must clicks to the beginning of the node text to select the node. If clicking in middle or in the end of the node text, then the node will not be selected.

  2. If I change tvItemsGetText to below, then the problem disappears:

    procedure TForm1.tvItemsGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
    var
      PData: ^TMyData;
    begin
      CellText := 'This is a test node';
    end;

I set a breakpoint in tvItemsGetText and find it will be invoked several times. At the first several times, the PData will be nil, which makes the CellText empty. At the final invokation, the PData will become valid and the CellText will be set to 'This is a test node'.

It seems that the range that allow mouse click and select the node is determined by the initial texts of the node. If the initial text is empty string, then one must click at the very beginning of the node to select it.

Is this a bug of Virtual Tree View?


Solution

  • There are several ways to init new node by user data.

    1. Using OnInitNode event:

    procedure TForm5.Button1Click(Sender: TObject);
    begin
      vt1.InsertNode(nil, amAddChildLast); // internal calls vt1InitNode
    end;
    
    procedure TForm5.vt1InitNode(Sender: TBaseVirtualTree; ParentNode, Node: PVirtualNode;
      var InitialStates: TVirtualNodeInitStates);
    var
      PData: ^TMyData;
    begin
      PData := Sender.GetNodeData(Node);
      PData^.Caption := 'This is a test node';
    end;
    

    2 Using UserData param

    Variant 1. Dynamic data

    Do not forget to remove InitNode event and dont set NodeDataSize property

    type
      TMyData = record
        Caption: string;
      end;
      PMyData = ^TMyData;
    
    procedure TForm5.Button1Click(Sender: TObject);
    var
      p: PMyData;
    begin
      New(p);
      p.Caption:='This is a test node'; 
      vt1.InsertNode(nil, amAddChildLast, p); // create node with initialized user data
      // by default VirtualTree use NodeDataSize = SizeOf(pointer), 
      // so there is no reason to use GetNodeDataSize event 
    end;
    
    
    procedure TForm5.vt1GetText(Sender: TBaseVirtualTree; Node: PVirtualNode; 
      Column: TColumnIndex; TextType: TVSTTextType;
      var CellText: string);
    var
      PData: PMyData;
    begin
      if Assigned(Node) then
      begin
        PData := PMyData(Sender.GetNodeData(Node)^); // little modification
        // for correct access to dynamic node data
    
        if Assigned(PData) then
          CellText := PData.Caption;
      end;
    end;
    
    procedure TForm5.vt1FreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
    var
      p: PMyData;
    begin
      p:=PMyData(Sender.GetNodeData(Node)^);
      Dispose(p); // as you allocate memory for user data - you should free it to avoid memory leaks
    end;
    

    Variant 2. Objects

    Add new private function to your form:

      private
        { Private declarations }
        function GetObjectByNode<T: class>(Node: PVirtualNode): T; 
        // do not forget to include System.Generics.Collections to `uses`
    

    realization:

    function TForm5.GetObjectByNode<T>(Node: PVirtualNode): T;
    var
      NodeData: Pointer;
      tmpObject: TObject;
    begin
      Result := nil;
      if not Assigned(Node) then
        exit;
      NodeData := vt1.GetNodeData(Node);
      if Assigned(NodeData) then
        tmpObject := TObject(NodeData^);
    
      if tmpObject is T then
        Result := T(tmpObject)
      else
        Result := nil;
    end;
    

    And the main code (almost identical to variant 1):

    procedure TForm5.Button1Click(Sender: TObject);
    var
      d: TMyData;
    begin
      d := TMyData.Create;
      d.Caption := 'This is a test node';
      vt1.InsertNode(nil, amAddChildLast, d);
    end;
    
    procedure TForm5.vt1FreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
    var
      d: TMyData;
    begin
      d := GetObjectByNode<TMyData>(Node);
      d.Free;
    end;
    
    procedure TForm5.vt1GetText(Sender: TBaseVirtualTree; Node: PVirtualNode; 
      Column: TColumnIndex; TextType: TVSTTextType;
      var CellText: string);
    var
      d: TMyData;
    begin
      d := GetObjectByNode<TMyData>(Node);
      if Assigned(d) then
        CellText := d.Caption;
    end;