Search code examples
delphiinfinite-scrollvclvirtualtreeview

Infinite scrolling VirtualTreeView


Is there a way to implement infinite scrolling using virtualtreeview?

I'd like to load a set number of database records at a time, and add them to the virtualtreeview when the user scrolls down. But I'm not sure how I would trigger the adding of new rows.


Solution

  • You can handle the OnScroll event and check if the scrollbar reached the end this way:

    type
      // this interposer class is used to publish the RangeY property
      TVirtualStringTree = class(VirtualTrees.TVirtualStringTree)
      public
        property RangeY;
      end;
    
    procedure TForm1.VirtualStringTreeScroll(Sender: TBaseVirtualTree; DeltaX,
      DeltaY: Integer);
    var
      Tree: TVirtualStringTree;
    begin
      // if the vertical scroll occurred, then...
      if DeltaY <> 0 then
      begin
        // just a helper variable
        Tree := TVirtualStringTree(Sender);
        // if the client height without the top offset equals, or exceeds (actually, it should
        // never exceed; just for sure) the virtual tree height, then we reached the bottom of
        // the tree, so...
        if Tree.ClientHeight - Tree.OffsetY >= Integer(Tree.RangeY) then
        begin
          // the scrollbar reached the end of the tree; now fetch your data and add some nodes
          // (ideally as a thread task showing some fancy animation; the following is just for
          // example)
          ShowMessage('Fetch your data...');
          Tree.RootNodeCount := Tree.RootNodeCount + 50;
        end;
      end;
    end;